从7.2升级到PHP 7.4后得到 "Trying to access array offset on value of type bool in on line... "错误。

问题描述 投票:0回答:1
$useremail = $_POST['txt_email'];
$password  = $_POST['txt_password'];

$select = $pdo->prepare("SELECT * FROM tbl_user WHERE useremail='$useremail' AND password='$password'");

$select->execute();

$row=$select->fetch(PDO::FETCH_ASSOC);

if($row['useremail']==$useremail AND $row['password']==$password AND $row['role']=="Admin"){ // Line 27
    // .. rest of the code
}

$row 是false,当提交了一个无效的凭证。否则,它返回一个数组。

这在 PHP v7.2 时工作得很好,但升级到 v7.4 后,我得到了这样的通知:"我的代码是一个数组。

Notice: Trying to access array offset on value of type bool in index.php on line 27
php upgrade isset
1个回答
1
投票

有可能没有记录返回,所以你需要检查一下,你可以在尝试访问任何索引之前测试变量是否有值。

if($row){
  //add your code
}
© www.soinside.com 2019 - 2024. All rights reserved.