Jump to content

mysqli_num_rows Returns An Incorrect Number Of Rows


JustinK101

Recommended Posts

Ok, I am not sure what's going on here, maybe a bug but here is the code:

 

$sql = "SELECT count(email)
		            AS counter 
				  FROM users
				 WHERE email = '" . $email . "'
				   AND password = '" . $password . "'";

$result = mysqli_query($sql) or die("Oops");
echo mysqli_num_rows($result);

 

Consistently returns 10, but there is only 1 row returned. the table users has a primary key of email, so there can only be one row with a given email address. Something very strange going on.

 

 

...mmm, from my perspective your query seems OK, but for the heck of trouble shooting change:

 

count(email)

 

to:

 

*

 

or:

 

email

 

 

Also, does

echo mysqli_num_rows($result);

prints 10, is that what you meant?

 

Run the query on the shell; Worse case scenario I'll think you have a design logic problem.

 

good luck

 

Angel.

 

The result is unbuffered so the actual count will not be returned until you retrieve all the rows...

 

change...

 

$result = mysqli_query($sql) or die("Oops");

 

to...

$result = mysqli_query ( $sql, MYSQLI_STORE_RESULT ) or die ( "Oops" );

 

 

Then mysli_num_rows() will work right. When doing COUNT() always use * MYSQLI_STORE_RESULT *, when returning bigger result sets that you need to while(loop) always use * MYSQLI_USE_RESULT *

 

Other notes...

 

When you do a COUNT(?) you are guaranteed a result, so it's better to do...

 


/* procedural style */

$result = mysqli_query ( 'SELECT COUNT(field_to_count_or_*) AS total FROM table...', MYSQLI_STORE_RESULT );

$found = mysqli_fetch_assoc ( $result );

echo $found['total'];

 

 

printf,

 

That's crazy you have to add an extra parameter to mysqli_query() for mysqli_num_rows() to work properly. So I have all this logic stored in a db abstraction function, how do I determine if I want to use MYSQLI_STORE_RESULT or MYSQLI_USE_RESULT? Could I just always use MYSQLI_STORE_RESULT without problems/performance hits?

 

By the way, does this only apply to mysqli, if I was using mysql standard would the mysql_num_rows() work fine? Thanks.

Yeah, I always thought it was not the query;

Problem resides in either PHP code or MySQL stored data/table design.

As I said earlier, try running the query from command line.

 

If result is as it should, then it is a PHP code problem, otherwise MySQL problem.

Try echoing the query just before running it

 

$sql = "SELECT count(email)
                     AS counter
                 FROM users
                WHERE email = '" . $email . "'
                  AND password = '" . $password . "'";

echo $sql;
$result = mysqli_query($sql) or die("Oops");
echo mysqli_num_rows($result);

 

you will see if the query is well formed. You can even run it from console or phpMyAdmin to see if it will return 1 row only

Scouring the net, I found this:

 

"The use of mysqli_num_rows() depends on whether you use buffered or unbuffered result sets. In case you use unbuffered resultsets mysqli_num_rows() will not correct the correct number of rows until all the rows in the result have been retrieved. "

 

So this seems to be the case, something related to the unbuffered result. Very odd though. When I get home tonight I will do more tests.

Is it just me being stupid, or has SQL always worked this way:

 

SELECT COUNT() as counter

    will always return a single row that countains the count value of rows that match the selection criteria.

mysqli_num_rows($result) will give 1, and the value of that single result (counter) will be 10

 

SELECT *

    will return the actual rows of data that match the selection criteria.

mysqli_num_rows($result) will give 10

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.