943,634 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 9556
  • PHP RSS
May 7th, 2009
6

FAQ: "Supplied argument not valid resource"

Expand Post »
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:
php Syntax (Toggle Plain Text)
  1. $conn = mysql_connect('host', 'user', 'pass');
  2. if(!$conn) {
  3. // echo or use die() here, your choice.
  4. echo mysql_error();
  5. }

2) Your database select statement should look similar:
php Syntax (Toggle Plain Text)
  1. $dbselect = mysql_select_db('dbname');
  2. if(!$dbselect) {
  3. // once again, echo or die your choice
  4. echo mysql_error();
  5. }

3) Finally do the same for your mysql_query() call:
php Syntax (Toggle Plain Text)
  1. $result = mysql_query('SELECT * FROM some_table WHERE blah = "blah"');
  2. if(!$result) {
  3. echo mysql_error();
  4. }

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.
Last edited by peter_budo; May 8th, 2009 at 5:31 pm. Reason: Edit by request
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
May 8th, 2009
2

Re: FAQ: "Supplied argument not valid resource"

Also, you may want to note that the following script commonly used will cause an error on some servers:
php Syntax (Toggle Plain Text)
  1. //mysql connections
  2.  
  3. $result=mysql_query('SELECT * FROM `table`');
  4. while ($row=mysql_fetch_array($result)) {
  5. echo 'test didn\'t work. There shouldn\'t be any rows in the table.';
  6. break;
  7. }
Instead the following should be used:
php Syntax (Toggle Plain Text)
  1. //mysql connections
  2.  
  3. $result=mysql_query('SELECT * FROM `table`');
  4. if (mysql_num_rows($result)>0) {
  5. while ($row=mysql_fetch_assoc($result)) {
  6. echo 'test didn\'t work. There shouldn\'t be any rows in the table.';
  7. break;
  8. }
  9. }
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
May 8th, 2009
1

Re: FAQ: "Supplied argument not valid resource"

Click to Expand / Collapse  Quote originally posted by cwarn23 ...
Also, you may want to note that the following script commonly used will cause an error on some servers:
php Syntax (Toggle Plain Text)
  1. //mysql connections
  2.  
  3. $result=mysql_query('SELECT * FROM `table`');
  4. while ($row=mysql_fetch_array($result)) {
  5. echo 'test didn\'t work. There shouldn\'t be any rows in the table.';
  6. break;
  7. }
Instead the following should be used:
php Syntax (Toggle Plain Text)
  1. //mysql connections
  2.  
  3. $result=mysql_query('SELECT * FROM `table`');
  4. if (mysql_num_rows($result)>0) {
  5. while ($row=mysql_fetch_assoc($result)) {
  6. echo 'test didn\'t work. There shouldn\'t be any rows in the table.';
  7. break;
  8. }
  9. }
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.
Last edited by kkeith29; May 8th, 2009 at 6:53 am.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
May 31st, 2009
1

Re: FAQ: "Supplied argument not valid resource"

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
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:

PHP Syntax (Toggle Plain Text)
  1. foreach($results as $result) {}

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

PHP Syntax (Toggle Plain Text)
  1. 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:

PHP Syntax (Toggle Plain Text)
  1. if (is_array($results)) {
  2. foreach($results as $result) {}
  3. }

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

PHP Syntax (Toggle Plain Text)
  1. if ($results) {
  2. foreach($results as $result) {}
  3. }

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in PHP Forum Timeline: Saving mage in a path instead of storing the real image in mysql
Next Thread in PHP Forum Timeline: Problem With My Website Design





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC