0

I´ve two php scripts: main.php and script.php.

I´m sending a variable $id from main.php to script.php. In the script.php file I´m using this variable $id to get certain data out of a MySQL database. This data is stored in the variable $bonusspellid and needs to send back to the main.php file.

For transfering the variables between the two files I´m using session_start() but I get a "503 GET error" when opening the main.php file.

What is the right way to transfer the both variables between the scripts?

main.php

session_start(); 

$id = "458";

$_SESSION['id'] = $id;

// receive variable from script.php:

$bonusspellid = $_SESSION['bonusspellid'];

echo $bonusspellid;

script.php

session_start(); 

// receive variable from main.php:

$id = $_SESSION['id'];

$conn = new mysqli("localhost", "xxxxx", "xxxxxx", "xxxxxx");
$conn->set_charset("ISO-8859-1");

$sqlitemeffect = "SELECT * FROM ItemEffect WHERE ID IN (?)";

$stmt60 = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt60, $sqlitemeffect)) {
    echo "SQL Failed";
} else {

    mysqli_stmt_bind_param($stmt60, "s", $id);

    mysqli_stmt_execute($stmt60);
    $resultitemeffect = mysqli_stmt_get_result($stmt60);

    while($rowitemeffect = mysqli_fetch_assoc($resultitemeffect)) {
        $rowsitemeffect[] = $rowitemeffect;
     }
}

$bonusspellid = $rowsitemeffect['0']['SpellID'];

 // Send variable to main.php 
 
$_SESSION['bonusspellid'] = $bonusspellid;
7
  • Session_start should be at the beginning of the php. Everything else after. Commented Mar 22, 2021 at 14:02
  • I´ve changed it but no effect. Commented Mar 22, 2021 at 14:05
  • How do you open the files? Are there any errors there? I cannot find where you open/call the other php-file Commented Mar 22, 2021 at 14:05
  • These are the complete scripts. So maybe that´s the mistake. How do I call the other php file? Commented Mar 22, 2021 at 14:10
  • You can call header("Location:script.php"); exit(); (and similar for main.php). Note that these header-calls are not "returning". So you won't get back to the calling point afterwards. It simply opens another php-file and executes it Commented Mar 22, 2021 at 14:12

1 Answer 1

1

You don't need session variables for your code. Try this:

// main.php
require_once("script.php");
$id = "458";

$bonusspellid = get_bonus_spell($id);;

echo $bonusspellid;

And then your script.php:

// script.php
function get_bonus_spell($id) {
   $conn = new mysqli("localhost", "xxxxx", "xxxxxx", "xxxxxx");
   $conn->set_charset("ISO-8859-1");

   $sqlitemeffect = "SELECT * FROM ItemEffect WHERE ID IN (?)";

   $stmt60 = mysqli_stmt_init($conn);

   if (!mysqli_stmt_prepare($stmt60, $sqlitemeffect)) {
       echo "SQL Failed";
   } else {
       mysqli_stmt_bind_param($stmt60, "s", $id);
       mysqli_stmt_execute($stmt60);
      $resultitemeffect = mysqli_stmt_get_result($stmt60);
 
      while($rowitemeffect = mysqli_fetch_assoc($resultitemeffect)) {
         $rowsitemeffect[] = $rowitemeffect;
      } 
   }

   $bonusspellid = $rowsitemeffect['0']['SpellID'];

   return $bonusspellid;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.