mysql_num_rows() returns 1 when there are no rows???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dashifen
Forum Commoner
Posts: 35
Joined: Thu Feb 05, 2004 9:53 pm
Location: Champaign - Urbana, IL

mysql_num_rows() returns 1 when there are no rows???

Post 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?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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).
User avatar
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???

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
Post Reply