ログイン システムをセットアップしようとしていますが、データベースへの 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 を実行すると動作します。しかし、その理由がわかりません。SELECT ステートメントで fetch() を 1 行に制限しているため、むしろ fetch() を使用したいと思います。
どんな提案でも大歓迎です。