We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion   Closed to New Replies

FAQ: "Supplied argument not valid resource"

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.

4
Contributors
3
Replies
3 Weeks
Discussion Span
3 Years Ago
Last Updated
9
Views
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 269
Skill Endorsements: 25

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;
    }
}
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 260
Skill Endorsements: 13

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.

kkeith29
Nearly a Posting Virtuoso
1,388 posts since Jun 2007
Reputation Points: 250
Solved Threads: 202
Skill Endorsements: 6

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.

digital-ether
Nearly a Posting Virtuoso
Team Colleague
1,295 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
Skill Endorsements: 9
 
© 2013 DaniWeb® LLC
Page rendered in 0.0944 seconds using 2.79MB