Why is PDO’s fetchColumn() ignoring the SQL LIMIT part of my query?
$result returns “2” when there are 2 records found in the database?
Thanks!
$sql = "SELECT count(*) FROM table WHERE Username = ? and
Password = ? LIMIT 1";
$q = $this->connection->prepare($sql);
$q->execute(array($user, $pwd));
$result = $q->fetchColumn();
echo $result;
Because the count may be 2 for the number of records found in the database but that aggregate function only requires one row in which to return the result. The limit here is actually useless.
In other words, go run that query without the LIMIT on your server and you will see a one row result set with the value of 2 (if there are two such user/password combos in the table that is).