我正在用PHP创建用户登录名。我编写了以下代码来从数据库中获取一些字段。 id和user_role是整数,是设置为会话变量时唯一返回null的字段。我不知道是什么问题。
$q = "SELECT id, password, last_name, user_role, first_name FROM users WHERE email=?";
$statement = $con->prepare($q);
$statement->bind_param("s", $email);
$statement->execute();
$result = $statement->get_result();
if ($result->num_rows > 0) {
$result = $result->fetch_assoc();
$password_hash = $result['password'];
if (passwordVerify($password, $password_hash)) {
$_SESSION['user_role'] = $result['user_role']; // set user role to session
$_SESSION['email'] = $email;
$_SESSION['id'] = $result['id'];
$_SESSION['last_name'] = $result['last_name'];
$_SESSION['first_name'] = $result['first_name'];
header('Location: index.php');
} else {
$_SESSION['msg'] = "Invalid Password/ Email Address";
}
} else {
$_SESSION['msg'] = "Invalid Password/ Email Address";
}
当我回显$_SESSION['email']
时,它显示电子邮件,但是当我回显$_SESSION['id']
时,它什么也不显示。
echo $result['id']
显示ID。
修改
采样var_dump($result);
和var_dump($_SESSION);
输出
array (size=5)
'user_role' => int 2
'email' => string 'sample@gmail.com' (length=16)
'id' => int 30
'last_name' => string 'Doe' (length=3)
'first_name' => string 'John' (length=4)
echo $_SESSION['email']
输出sample@gmail.com
echo $_SESSION['id']
为空