mysql_num_rows Driving me Mad!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

mysql_num_rows Driving me Mad!

Post by Gen-ik »

I've been through as many PHP forum posts as I can to try and sort this out but nothing seems to work!

I get this error...

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result

...from this code...

$result=mysql_query("SELECT * FROM mytable WHERE username=$userNAME AND userpass=$userPASS");

$num_results=mysql_num_rows($result);

if($num_results==0)
{
echo("No results found");
}
else
{
echo("Results found");
}


...can anyone see a problem here?

Thanks. :lol:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You probably need to quote $userNAME and $userPASS like

Code: Select all

$result=mysql_query("SELECT * FROM mytable WHERE username='$userNAME' AND userpass='$userPASS'");
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I've tried that... I've even gone as far as replacing the whole line with the word Banana!

It doesn't even work if I replace the $userNAME and $userPASS with words instead of variables.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Can you check the mysql error log? That should provide more insight into why the query doesn't work. Does mytable exist, does it have fields named username and password. Does the user you are connecting as have permission to select from that table? Do you have other queries that work?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I can write to the database fine.. I can add a new username and userpass into the database and it shows up ok.

All I'm trying to do is check is username & userpass exsist in the database for a simple log-on script.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

STOP PRESS!

I now works fine!!! I don't know why or how it's working but it is.. and I haven't changed anything.

Thanks for the help anyway.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah, so it's a heisenbug: You cannot know both: why or when it occurs :D
Post Reply