mysql num rows error

They have: 461 posts

Joined: Jul 2003

problem:

Quote: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/findyourdesire/stats.php on line 21

section of code causing it:

<?php
# talk to the db
$db=mysql_connect($host, $login1, $pass1) or die(\"cannot access mysql\"); # connect
$fyd=mysql_select_db('findyourdesire', $db) or die(\"cannot access db\"); # select the db
$querystats=mysql_query(\"SELECT * FROM stats WHERE uid='$uin'\", $db); # find the stats
$stats=mysql_fetch_array($querystats); # get the stats
$queryvotes=mysql_query(\"SELECT * FROM votes WHERE votee='$uin'\", $db); # find the votes
$vc=mysql_num_rows($queryvotes); # how many votes
for(
$i=0;$i<$vc;$i++){ # for each vote
 
$voteinfo=mysql_fetch_array($queryvotes); # get the vote info
 
$vt+=$voteinfo['vote']; # find the vote total
}
if(
$vc==0){ $vc=1; }//prevent div by zero issue
?>
i don't understand how $queryvotes can be anything but a valid sql query resource when $querystats is one

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Try testing it against FALSE. If the query failed, for whatever reason, mysql_query() will return false.

He has: 1,016 posts

Joined: May 2002

Before the mysql_num_rows() line, type: echo mysql_error();
If there are any problems, that line will print it on the screen.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

I think I've said this before, but instead of forcing someone else to go through my code line by line, I use:

<?php
if (!$qry) {
    echo
mysql_error();
    exit;
    }
?>

Perhaps there is a better way to check line by line, but that's often all I need after a particular query to find MY ERROR and fix it.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

"When in doubt, print it out."

I think often times there is nothing wrong with the logic where the error occurs. But rather, the data we are manipulating is not what we expected it to be at that point.

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.