Page 1 of 1

mysql_num_rows not working return 0

Posted: Sat Jul 22, 2006 2:40 am
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]

Posted: Sat Jul 22, 2006 2:51 am
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;
}

Posted: Sat Jul 22, 2006 3:51 am
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..

Posted: Sat Jul 22, 2006 4:03 am
by Benjamin
Is that you Oren?

Posted: Sat Jul 22, 2006 4:07 am
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...

Posted: Sat Jul 22, 2006 4:14 am
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

Posted: Sat Jul 22, 2006 4:25 am
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.

Posted: Sat Jul 22, 2006 4:39 am
by Benjamin
Probaby an issue with $date then..

Posted: Sat Jul 22, 2006 5:44 am
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.

Posted: Sat Jul 22, 2006 5:58 am
by Benjamin
Paste the query

Posted: Sat Jul 22, 2006 6:08 am
by victor
long live astion, it's working now.

thanks a billion.

Posted: Sat Jul 22, 2006 6:09 am
by Benjamin
Did you copy that code up there or what?

Posted: Mon Jul 24, 2006 3:03 am
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.