PHP 7.2 - 警告:count():参数必须是一个数组或一个实

PHP 7.2 - Warning: count(): Parameter must be an array or an object that implements Countable(PHP 7.2 - 警告:count():参数必须是一个数组或一个实现 Countable 的对象)
本文介绍了PHP 7.2 - 警告:count():参数必须是一个数组或一个实现 Countable 的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的 PHP 安装从 5.6 升级到了 7.2.我在登录页面上使用了 count() 函数,如下所示:

I just upgraded my PHP installation from version 5.6 to 7.2. I used the count() function on my login page like so:

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有关,我找不到纯PHP的解决方案.

After searching, I found questions and answers similar to this one, but they all were related to WordPress, and I couldn’t find a solution for Pure PHP.

推荐答案

PDO fetch 在失败时返回 false.所以你也需要检查这个案例:

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

这篇关于PHP 7.2 - 警告:count():参数必须是一个数组或一个实现 Countable 的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)