提问人:Ryan 提问时间:5/11/2020 最后编辑:DharmanRyan 更新时间:5/11/2020 访问量:302
尝试访问 null sql 错误类型值的数组偏移量
Trying to access array offset on value of type null sql error
问:
我的表中有一个名为“已验证”的布尔列。我正在尝试检查 verified 是否等于 0 “false”,如果是,我想显示我的错误。下面是代码行
// If User Click the Login Button
if(isset($_POST['logbtn'])){
$username = $_POST['username'];
$password = $_POST['password'];
$verified = "";
$sql = "SELECT * FROM users WHERE verified=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('b', $verified);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$_SESSION['verified'] = $user['verified'];
if($_SESSION['verified'] == 0){
$errors['verified'] = "Please verify your Email";
}
我没有检查列是否为 0,而是收到错误“尝试访问 null 类型值的数组偏移量”
答:
-2赞
Josep Widtantio
5/11/2020
#1
你可以使用 所以如果值为 0 / null / 空字符串 / false ,它将返回 trueif (empty($_SESSION['verified']))
$user['verified']
1赞
Dharman
5/11/2020
#2
您尚未向我们显示完整的错误消息,但很可能是没有从数据库返回的记录。您应该改为检查单个用户记录。
if (isset($_POST['logbtn'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $username);
$stmt->execute();
$result = $stmt->get_result();
if ($user = $result->fetch_assoc()) {
$_SESSION['verified'] = $user['verified'];
} else {
// There is no such user in the database.
$_SESSION['verified'] = false;
}
if (!$_SESSION['verified']) {
$errors['verified'] = "Please verify your Email";
}
}
我更改了您的 SQL 以获取仅与该用户名匹配的记录(假设它是唯一的用户标识符),然后我检查是否从数据库中获取了任何内容。我还更改了mysqli类型,因为在这种情况下您不应该使用二进制类型。s
评论
s
b