在php脚本之间发送变量
我有两个php脚本:main.php和script.php 我正在将一个变量在php脚本之间发送变量,php,Php,我有两个php脚本:main.php和script.php 我正在将一个变量$id从main.php发送到script.php。 在script.php文件中,我使用这个变量$id从MySQL数据库中获取某些数据。此数据存储在变量$bonusspellid中,需要发送回main.php文件 为了在两个文件之间传输变量,我正在使用session\u start(),但在打开main.php文件时,我得到了一个“503 get error” 在脚本之间传输这两个变量的正确方法是什么 main.php
$id
从main.php发送到script.php。
在script.php文件中,我使用这个变量$id
从MySQL数据库中获取某些数据。此数据存储在变量$bonusspellid
中,需要发送回main.php文件
为了在两个文件之间传输变量,我正在使用session\u start()
,但在打开main.php文件时,我得到了一个“503 get error”
在脚本之间传输这两个变量的正确方法是什么
main.php
session_start();
$id = "458";
$_SESSION['id'] = $id;
// receive variable from script.php:
$bonusspellid = $_SESSION['bonusspellid'];
echo $bonusspellid;
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;
script.php
session_start();
$id = "458";
$_SESSION['id'] = $id;
// receive variable from script.php:
$bonusspellid = $_SESSION['bonusspellid'];
echo $bonusspellid;
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;
代码不需要会话变量。试试这个:
// main.php
require_once("script.php");
$id = "458";
$bonusspellid = get_bonus_spell($id);;
echo $bonusspellid;
然后是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;
}
Session_start应该在php的开头。之后的所有内容。我已更改,但没有效果。如何打开文件?有什么错误吗?我找不到您在哪里打开/调用其他php文件这些是完整的脚本。所以,也许这是个错误。如何调用另一个php文件?可以调用
头文件(“Location:script.php”);退出()代码>(与main.php类似)。请注意,这些标头调用不是“返回”。所以你以后就回不到呼叫点了。它只是打开另一个php文件并执行它