If you've gotten the error "Supplied argument is not a valid MySQL resource" you're in luck because so has everyone else that has ever used PHP... ever. The error is caused by a number of things including: Your query is wrong, you failed to connect to the database or you selected the wrong database.

Now, before you go start modifying your code make sure you've used the correct username, password and host for your mysql_connect function and you've selected the correct database. If you've done this and you're still getting an error follow the steps below.

So to figure out which of these happened we're going to use the mysql_error() function which tells us what went wrong last:1) Your connection string should look something along the lines of the following:

$conn = mysql_connect('host', 'user', 'pass');
if(!$conn) {
  // echo or use die() here, your choice.
  echo mysql_error();
}

2) Your database select statement should look similar:

$dbselect = mysql_select_db('dbname');
if(!$dbselect) {
  // once again, echo or die your choice
  echo mysql_error();
}

3) Finally do the same for your mysql_query() call:

$result = mysql_query('SELECT * FROM some_table WHERE blah = "blah"');
if(!$result) {
  echo mysql_error();
}

If these steps don't solve your error use the Daniweb Search functionality to find other people with similar problems, I can guarantee you aren't the first.

Finally as a LAST resort, and I emphasize last because, as I said, your question has been asked before, post a new topic being as specific as possible about your problem. Post your code along with the exact error and line numbers. Most importantly, USE CODE TAGS. If you don't use code tags you will most likely be ignored by a large number of our members.

Hope this has been helpful and we all look forward to seeing you in the future.

Good Luck.

Edit: If you have any additions please PM and I will consider them. Do NOT PM me with your questions.

Edited 4 Years Ago by pritaeas: Re-stickified, closed to prevent replies

Comments
Good place for a beginner to start learning the basics of DB connection handling
Good work! :) I hope people read this before creating new threads with title "Error: Supplied argument is not a valid resource! Plzzz help.."
Great post. Whether people will look is a different matter...

Also, you may want to note that the following script commonly used will cause an error on some servers:

//mysql connections

$result=mysql_query('SELECT * FROM `table`');
while ($row=mysql_fetch_array($result)) {
    echo 'test didn\'t work. There shouldn\'t be any rows in the table.';
    break;
    }

Instead the following should be used:

//mysql connections

$result=mysql_query('SELECT * FROM `table`');
if (mysql_num_rows($result)>0) {
while ($row=mysql_fetch_assoc($result)) {
    echo 'test didn\'t work. There shouldn\'t be any rows in the table.';
    break;
    }
}

Also, you may want to note that the following script commonly used will cause an error on some servers:

//mysql connections

$result=mysql_query('SELECT * FROM `table`');
while ($row=mysql_fetch_array($result)) {
    echo 'test didn\'t work. There shouldn\'t be any rows in the table.';
    break;
    }

Instead the following should be used:

//mysql connections

$result=mysql_query('SELECT * FROM `table`');
if (mysql_num_rows($result)>0) {
while ($row=mysql_fetch_assoc($result)) {
    echo 'test didn\'t work. There shouldn\'t be any rows in the table.';
    break;
    }
}

It really doesn't matter since mysql_fetch_assoc() will evaluate to false if there are 0 rows, therefore the while loop will not run. No server should have errors with that basic code.

It really doesn't matter since mysql_fetch_assoc() will evaluate to false if there are 0 rows, therefore the while loop will not run. No server should have errors with that basic code.

True, there should be no problems with the while() loop.

It is good to note that if you use foreach() or for() iterators you should always check the type before iterating.

This isn't required in a while because while automatically converts all types to Boolean. So it can handle multiple types (both the Array and Boolean false). Iterators cannot.

The same code in a foreach() loop would get errors.

eg:

foreach($results as $result) {}

If $results is not an Array, it will give an error.

for($i = 0; $i < count($results); $i++) {}

If results is not an array, count will give you 1, even count(FALSE) is 1.

Correct way:

if (is_array($results)) {
foreach($results as $result) {}
}

I hardly see this used however. Instead is seems the common alternative used is:

if ($results) {
foreach($results as $result) {}
}

This works however if the results will always be false, or an Array. I'm sure a type check is faster then boolean conversion anyway.