mysql_num_rows() returns 1 when there are no rows???
Moderator: General Moderators
mysql_num_rows() returns 1 when there are no rows???
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?
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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).
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).
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;- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
Re: mysql_num_rows() returns 1 when there are no rows???
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