我想設置一個登錄系統,並且在檢查從我的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