I have this query:
$butacas= $this->pdo->prepare('SELECT COUNT( * ) FROM `usuarios` WHERE `sala` LIKE :nombreSala');
$butacas->bindValue(':nombreSala', $nombreSala);
$butacas->execute();
echo $butacas->rowCount();
This query results in an integer: 1
if I replace :nombreSala
with the value that actually exists in the database, like:
SELECT COUNT( * ) FROM usuarios WHERE sala LIKE 'salaChica'
It still results in 1.
Now, when checking the database with phpMyadmin I realise that with that value (salaChica) there are 2 items instead of one! (and consulting from phpMyadmin does result in 2).
Why it is not accurate? I've read this post about that function not being always accurate with SELECT, but is there a simple alternative?
You're confusing the number of rows returned (1) with the value returned (2). Your query "SELECT count(*) WHERE whatever" will always return 1 row (unless you do a group by). that row contains a single column - the value of that column will be the count - 2 in your case.
Try
echo $butacas->fetchColumn();
instead of
echo $butacas->rowCount();