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.

 

 

Link to comment
Share on other sites

...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.

 

Link to comment
Share on other sites

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'];

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.