Page 1 of 1
mysql_num_rows() returns 1 when there are no rows???
Posted: Tue Aug 31, 2004 9:29 am
by dashifen
Running PHP 4.3.8 and for some reason, mysql_num_rows() seems to return 1 if there are no rows return. It returns the correct count when there is data, but if there's no information returned from the database, it still results in 1. I've gotten around this so far by making a call to the database for COUNT(<insert_database_field_here>) useing the exact same WHERE clause which works fine, but I'd prefer not to have the overhead of two database calls for each SELECT statement. Anyone else encounter this?
Posted: Tue Aug 31, 2004 10:14 am
by CoderGoblin
DEBUG HINT: If you are not getting a result you expect it is often a good idea to run the SQL yourself to ensure you are getting what you expect.
The SQL count(field) will always return a value even if it is 0.
You need to get the value of the count field to check against, not the num_rows value, but it sounds as though this is what you are doing. (An alternative below for others).
Code: Select all
SELECT field FROM table WHERE field like '%test%' limit 1;
If you are sure no rows are selected after performing the SQL yourself (I have never come across the problem), please post more information (The SQL statement and example table data and the appropriate PHP code).
Re: mysql_num_rows() returns 1 when there are no rows???
Posted: Tue Aug 31, 2004 4:58 pm
by itsmani1
dashifen wrote:Running PHP 4.3.8 and for some reason, mysql_num_rows() seems to return 1 if there are no rows return. It returns the correct count when there is data, but if there's no information returned from the database, it still results in 1. I've gotten around this so far by making a call to the database for COUNT(<insert_database_field_here>) useing the exact same WHERE clause which works fine, but I'd prefer not to have the overhead of two database calls for each SELECT statement. Anyone else encounter this?
well it cannot happen check ur query there must be some problem to it
Posted: Tue Aug 31, 2004 5:01 pm
by feyd
there's no problem. It's acting correctly. .. as CoderGoblin said, COUNT always returns a value. So, there is at least 1 row, returned when using COUNT (provided the query matches any rows)