当前位置: 首页 > 知识库问答 >
问题:

PHP7.2-警告:count():参数必须是实现Countable[closed]的数组或对象

焦苏燕
2023-03-14

我刚刚升级了我的PHP安装从版本

if (!empty($_POST['username']) && !empty($_POST['password'])):
    $records = $conn->prepare('SELECT id,username,password FROM users WHERE username = :username');
    $records->bindParam(':username', $_POST['username']);
    $records->execute();
    $results = $records->fetch(PDO::FETCH_ASSOC);

    $message = '';
    
    if (count($results) > 0 && password_verify($_POST['password'], $results['password'])) {
        $_SESSION['user_id'] = $results['id'];
        header("Location: /");
    } else {
        $message = 'Sorry, those credentials do not match';
    }
endif;

经过搜索,我找到了与此类似的问题和答案,但它们都与WordPress有关,我找不到Pure PHP的解决方案。

共有1个答案

吕扬
2023-03-14

PDOfetch失败时返回false。因此,您也需要检查此案例:

if ($results && count($results) > 0 && password_verify($_POST['password'], $results['password'])) {
    $_SESSION['user_id'] = $results['id'];
    header("Location: /");
} else {
    $message = 'Sorry, those credentials do not match';
}
 类似资料: