PHP 7.2 – 警告:count():参数必须是数组或实现Countable的对象
作者:互联网
我刚刚将PHP版本从5.6升级到7.2.我在登录页面中使用了count()函数,例如:
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的解决方案.
解决方法:
PDO fetch在失败时返回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';
}
标签:php,php-7-2 来源: https://codeday.me/bug/20190722/1500137.html