mysql_num_rows not working return 0

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
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

mysql_num_rows not working return 0

Post by victor »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Has anyone ran into problems where mysql_num_rows() return 0 when the result 
set clearly contains a certain number of rows? My code looks something 
like this

Code: Select all

$conn = db_connect();
$query = "select distinct id from table where MemberID='$ID' and Date='$today'";
$result = mysql_query($query);
if(!$result)
return false;
$num_reply = @mysql_num_rows($result);
if($num_reply==0)
return false;
else
return $num_reply;
$result shows Resource id #16, but $mysql_num_rows($result) still returns zero

any idea?


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You were missing a few brackets.. I cleaned it up a little for you.

Code: Select all

$conn = db_connect();

$query = "SELECT DISTINCT `id` FROM `table` WHERE `MemberID`='" . mysql_real_escape_string($ID) . "' AND `Date`='" . mysql_real_escape_string($today) . "'";

$result = mysql_query($query);

if (!$result) {
  return false;
}

$num_reply = @mysql_num_rows($result);

if ($num_reply==0) {
  return false;
} else {
  return $num_reply;
}
AngryPanda
Forum Newbie
Posts: 16
Joined: Wed Jul 19, 2006 12:18 am

Post by AngryPanda »

Those braces are just personal preference...

And there is "clearaly a certain number of rows" ... zero.

mysql_num_row() is not playing a trick on you.

Why do you think there are rows returned ? There aren't..
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Is that you Oren?
AngryPanda
Forum Newbie
Posts: 16
Joined: Wed Jul 19, 2006 12:18 am

Post by AngryPanda »

What ? Oren ? Assuming that is some previously banned character here or some business... ?

Well, no. I'm not, if that's what you're talking about.

It's just that those brackets are optional and that mysql_num_rows() is returning zero for a reason...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I believe brackets are good practice, he probably needed some backticks in the query.

http://dev.mysql.com/doc/refman/5.0/en/ ... words.html
AngryPanda
Forum Newbie
Posts: 16
Joined: Wed Jul 19, 2006 12:18 am

Post by AngryPanda »

I agree. I believe they are good practice as well.

But I don't think there's anything wrong with the query, either. OP said that it's returning a result resource, so the query isn't failing. It's just not returning any rows.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Probaby an issue with $date then..
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

The same query has return rows of result in mysqladmin, the date is a unix time stamp, I' ve tried adding brackets as suggested but doesn't help.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Paste the query
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

long live astion, it's working now.

thanks a billion.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Did you copy that code up there or what?
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

You're right, those backticks were needed in the query, had the little error on my syntax initially, the mysql_num_rows() return values after that.
Post Reply