PHP 7.2 - Warning: count(): Parameter must be an array or an object that implements Countable [closed]

不羁的心 提交于 2019-12-01 00:49:06

问题


I just upgraded my PHP Version from 5.6 to 7.2. I used count() function in my login page, example:

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;

After searching, I found questions and answers similar to this one but they were related to WordPress that they modified some files, but I couldn't find a solution for it using Pure PHP.


回答1:


PDO fetch returns false on failure. So you need to check this case too:

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';
}


来源:https://stackoverflow.com/questions/51594817/php-7-2-warning-count-parameter-must-be-an-array-or-an-object-that-impleme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!