我正在尝试设置登录系统,但无法检查从我的 PDO 调用返回到我的数据库的行数。当提供错误的用户名和/或密码时,以下代码应返回 0 行,但无论密码或用户名如何,它似乎都返回 1 行。
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);
当我执行 count($user = $check_login_stmt->fetchAll()) > 0 时,它可以工作。但我不明白为什么,我宁愿使用 fetch() 因为我将它限制在 SELECT 语句中的一行。
任何建议都非常感谢。