Jump to content
MakeWebGames

mysql_fetch_row(): supplied argument is not a valid MySQL result resource


Floydian
 Share

Recommended Posts

I've seen this one asked about so many times, it begs for some splainin...

 

There are a number of php functions that generate errors like this. mysql_fetch_row and mysql_fetch_array being some of the common ones.

So what exactly does this mean?

Well, these sorts of functions require a MySQL Result Resource to be passed to them. I'm putting MySQL Result Resource in bold and large font twice because I want to really emphasize this.

A MySQL Result Resource is what you get when you do a MySQL SELECT query. (Sure there are other ways to get a mysql result resource, but this is going to be the most common)

 

MySQL Result Resource

An example of a way to get an MRR (short for MySQL Result Resource for the rest of this thread) might go something like this:

 

$result = mysql_query('select blah from foo');

 

The mysql_query() function returns one of two things in this case.

  • It returns an MRR
  • It returns a boolean value of FALSE

 

This next section is the most important part of this post!

If your query could not be completed, you will get FALSE instead of an MRR. Now, since mysql_fetch_row() and mysql_fetch_array() require an MRR, you can't just plug the $result into these functions! This is because $result might be an MRR or it might be FALSE.

So, how do we compensate for this?

Glad you asked, because there are two things you want to do. Number one is you want to look for false values. If the $result is false, do an appropriate action, if it isn't false, carry on with what you wanted to do.

Then the second thing to do is, if $result is FALSE, find out why it is FALSE.

For the first part, all we have to do is this:

 

if ($result === false) {
echo "The last query failed!";
} else {
echo "The last query was successful";

// put your mysql_fetch_array() or mysql_fetch_row() here!

}

 

The second thing to do is:

We need to be able to debug our query, so we'll rewrite our query in order to make debugging easier.

 

// First, define your query and store just the query string
// in a variable. This allows us to print out the query string as well
// as use the query string in a query.
$query = "select blah from foo where userid = " . $userid;

// Now plug your query string into mysql_query();
$result = mysql_query($query);

// Now we need to find out if the query was successful
if ($result === false) {

// The query was not successful
// Display the query string along with a message.
echo "The last query failed!

The query was: " . $query;

} else {

// The query was successful, so carry on as normal
echo "The last query was successful";

// put your mysql_fetch_array() or mysql_fetch_row() here!

}

 

If you've been having problems with the sort of error I'm talking about, please take some time to study this post. If you have questions about it, feel free to post them and I'll do my best to answer them. Please keep your questions to the topic at hand. I am not offering to debug your scripts for you, but am offering to help you understand how to debug them yourself.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...