在我的代码中,它无法识别变量$userid
,此变量决定了所登录教师的ID,但在mysqli代码中,当我无法确定$userid
时回应它。但它确实知道用户已通过其$userid
登录。所以我的问题是,在mysqli中为什么无法找到$userid
?请查看mysqli bind_param()
中的注释,这就是问题所在。
以下是代码,包括php和mysqli:
我要说明member.php是包含$userid
信息的脚本:
<?php
session_start();
include('member.php');
...
function PickSession()
{
//Get data from database
$sessionquery = "
SELECT s.SessionId, SessionName, s.TeacherId
FROM Session s
INNER JOIN Session_Complete sc ON sc.SessionId = s.SessionId
WHERE
(ModuleId = ? AND Complete = ? AND s.TeacherId = ?)
ORDER BY SessionName
";
$complete = 1;
global $mysqli;
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("iii",$moduleId, $complete, $userid);//it doesn't recognse $userid
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbTeacherId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
echo $userid; //nothing displayed in this echo
...
if ((isset($username)) && (isset($userid))){ //user is successfully logged in
include('teachername.php');
...
<?php
ShowAssessment(); // Show information
}else{
echo "Please Login to Access this Page | <a href='./teacherlogin.php'>Login</a>";
}
?>
member.php脚本:
if (isset($_SESSION['teacherid'])) {
$userid = $_SESSION['teacherid'];
}
if (isset($_SESSION['teacherusername'])) {
$username = $_SESSION['teacherusername'];
}
?>
答案 0 :(得分:2)
您尚未初始化$userid
变量,您必须设置用于在查询中绑定变量的值,否则该变量的值将为NULL
。
$userid = "value";
$sessionqrystmt->bind_param("iii",$moduleId, $complete, $userid);
$userid
无法访问PickSession()
变量。因此,建议是将$userid
设为全局,或将其作为函数中的参数传递。
答案 1 :(得分:1)
可能是因为session_start()
缺失
答案 2 :(得分:0)
您无法在功能中访问$userid
。你可以做的事情:
将$userid
声明为全局变量(未推荐)
function PickSession() {
global $userid;
// and the rest of the function here
}
将$userid
作为参数传递给PickSession
答案 3 :(得分:0)
在PickSession
函数中,$userid
没有值,因为你没有定义它,你应该在调用它时将变量传递给函数,例如;
function PickSession($userid)
{
...
}
// Now call PickSession with the variable assigned:
PickSession($userid);