FAQ: "Supplied argument not valid resource"

Closed Thread

Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

FAQ: "Supplied argument not valid resource"

 
4
  #1
May 7th, 2009
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:
  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:
  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:
  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
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Quick reply to this message  
Join Date: Sep 2007
Posts: 1,452
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 135
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: FAQ: "Supplied argument not valid resource"

 
0
  #2
May 8th, 2009
Also, you may want to note that the following script commonly used will cause an error on some servers:
  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:
  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. }
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: FAQ: "Supplied argument not valid resource"

 
0
  #3
May 8th, 2009
Originally Posted by cwarn23 View Post
Also, you may want to note that the following script commonly used will cause an error on some servers:
  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:
  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.
Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: FAQ: "Supplied argument not valid resource"

 
0
  #4
May 31st, 2009
Originally Posted by kkeith29 View Post
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:

  1. foreach($results as $result) {}

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

  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:

  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:

  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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC