我想设置一个登录系统,并且在检查从我的PDO调用返回到我的数据库的行数时遇到问题。当提供错误的用户名和/或密码时,以下代码应该返回0行,但无论密码或用户名如何,它似乎都返回1行。计数PDO获取返回意外数
require_once("includes/database.php");
session_start();
$dbh = db_connect();
$response = array();
$user = $_POST['username'];
$password = $_POST['password'];
$check_login = "SELECT * FROM user WHERE user=:user AND password=:password LIMIT 1";
$check_login_stmt = $dbh->prepare($check_login);
$check_login_stmt->bindParam(":user", $user);
$check_login_stmt->bindParam(":password", $password);
if(!$check_login_stmt->execute()) {
$response['code'] = "failure";
$response['err'] = $check_login_stmt->errorInfo();
$response['reason'] = "bad_query";
} else {
if(count($user = $check_login_stmt->fetch()) > 0) {
$_SESSION['login'] = true;
$_SESSION['pb_committee'] = $user['pb_committee'];
$response['code'] = "success";
$response['count'] = count($user);
} else {
$response['code'] = "failure";
$response['reason'] = "bad_reqs";
}
}
echo json_encode($response);
当我做一个计数($ user = $ check_login_stmt-> fetchAll())> 0它的作品。但我不明白为什么,我宁愿使用fetch(),因为我将它限制在SELECT语句中的一行。
任何建议,非常感谢。
那一行包含什么? – hjpotter92
@GiantofaLannister当我添加print_r($ user)时,它只是说“真” – jrubins