PHP 7.2-警告:count():参数必须是实现Countable的数组或对象 [英] PHP 7.2 - Warning: count(): Parameter must be an array or an object that implements Countable
本文介绍了PHP 7.2-警告:count():参数必须是实现Countable的数组或对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚将我的 PHP版本从 5.6 升级到了 7.2 .我在登录页面中使用了count()
函数,例如:
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;
搜索后,我发现了与此问题类似的问题和答案,但是它们与WordPress有关,他们修改了一些文件,但是我找不到使用 Pure PHP 的解决方案.
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.
推荐答案
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的数组或对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文