Jump to content


Photo

mysql_num_rows(): returns invalid result resource


  • Please log in to reply
5 replies to this topic

#1 Pawige

Pawige
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 October 2006 - 02:17 AM

Here's the snipped out part of the code I'm using:

$dbh = mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database");

$movequery = "SELECT * FROM Gallery WHERE category=$category";
$moveresult = mysql_query($movequery);
$query = "SELECT * FROM Gallery WHERE id=$id AND category=$category";
$result = mysql_query($query);
mysql_close();

$numrows = mysql_num_rows($moveresult);

if ( $id == $numrows ) { $nextid=1; } else { $nextid=$id + 1; }
if ( $id == 1 ) { $previd=$numrows; } else { $previd=$id - 1; }

$row = mysql_fetch_row($result);

Running it at:

http://[my url]/gallery.php?id=3&category=0

It gives me:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /[my url]/gallery.php on line 18

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /[mu url]/gallery.php on line 23

Everything works fine if I change it to something like this:

$movequery = "SELECT * FROM Gallery WHERE category=1";
$moveresult = mysql_query($movequery);
$query = "SELECT * FROM Gallery WHERE id=1 AND category=1";
$result = mysql_query($query);
mysql_close();

So I'm guessing it has something to do with how I'm referencing those variables, but I'm totally stumped, because it used to work, and then suddenly stopped, and I hadn't touched any of the code in the interim.

I'm pretty new at this, primarily an artist, just trying to learn some programming skills on the side, so if I've made an idiotic error, you can just chalk it up to that...

Thanks in advance for any help!

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 18 October 2006 - 02:52 AM

After each query, check for errors like this:

$query = "SELECT * FROM Gallery WHERE id=$id AND category=$category";
$result = mysql_query($query);
if (!$result) die("Error in $query: " . mysql_error());

I'm also not sure if you can call mysql_close() so soon.  Maybe you should move it until after you have fetched the results.

That added line will tell you two things
- It will show you the query you are trying to execute.
- It will show you the error message that caused the query to fail (if it did fail)

#3 Pawige

Pawige
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 October 2006 - 03:34 AM

Now it says:

Error in SELECT * FROM Gallery WHERE id= AND category=: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND category=' at line 1

Here's what I could find about the server version, not sure if it's the right information, but it seems likely:

MySQL - 4.1.21-standard-log
Protocol version: 10
Server: Localhost via UNIX socket

I'm digging around to see if I can find the answer, but if somebody knows it or where to find it, that'd be nice, as I'm having some trouble finding anything helpful so far.

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 18 October 2006 - 04:36 AM

it's because $id and $category are undefined, thus they appear empty in your query.  MySQL won't tell you much more than "i'm expecting you to actually provide a value to compare against."  the problem is in your PHP.

since you're getting 'id' and 'category' from the URL, you'll need to access them using the $_GET superglobal unless register_globals is set to on, which for your sake i hope it isn't:

$movequery = "SELECT * FROM Gallery WHERE category={$_GET['category']}";
$query = "SELECT * FROM Gallery WHERE id={$_GET['$id']} AND category={$_GET['category']}";

give those a whirl.  it's also good practice to treat those values passed in the URL with a little caution (encasing them in single quotes in the query is a good starter).

#5 Pawige

Pawige
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 October 2006 - 04:45 AM

Thanks a lot for the quick replies! Works like a charm now.

Edit: Actually, I've got another quick question, how can I use this $_GET to grab the values from the URL and stick them into a local value? Something along the lines of:

$querylocal = $_GET['urlquery']

Which doesn't seem to work, but I imagine there must be some way to do it. I only need to because I've got a couple variables that I get from the URL and use in numerous places and in a couple of equations.

$querylocalblah = $querylocal - 1;

Something like that.

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 19 October 2006 - 04:13 PM

Definitely quote this literal values, or you're just asking for SQL injection troubles.  And I can't see why you wouldn't be able to do a single assignment, but I'm not no PHP expert.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users