Orionss Orionss - 1 year ago 116
PHP Question

SELECT COUNT(*) returns 0 even if the request should return 1

When I'm trying to get the number of rows on a SQL request and if not, the connection that follow fails.
But in any case (also if the request should return 1), it returns 0.
Here's my code :

$str = "SELECT id FROM admins WHERE courriel = ? AND mdp = ?";
$arr = array($mail, $pass);
$rqt = sendRqt($str, $arr);
$tab = $rqt->fetchAll();
echo count($tab);

I don't understand why there's no time it returns 1

(Here is my new code, I changed my request to log count($tab) and know it returns 0...)

Answer Source

The problem is the use of the php function count().

You already have the correct number in your $tab variable as a string (probably, depends on php configuration / version) so you can echo it or cast it to an integer to make sure it is a number.

However, in php:

count(0) === 1
count('0') === 1

See here for example.

You should remove count($tab).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download