简体   繁体   中英

Send variables between php scripts

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;

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM