| | |
FAQ: "Supplied argument not valid resource"
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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
So to figure out which of these happened we're going to use the
1) Your connection string should look something along the lines of the following:
2) Your database select statement should look similar:
3) Finally do the same for your mysql_query() call:
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.
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)
$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:
php Syntax (Toggle Plain Text)
$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:
php Syntax (Toggle Plain Text)
$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.
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.
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.
Also, you may want to note that the following script commonly used will cause an error on some servers:
Instead the following should be used:
php Syntax (Toggle Plain Text)
//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; }
php Syntax (Toggle Plain Text)
//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; } }
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
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
•
•
•
•
Also, you may want to note that the following script commonly used will cause an error on some servers:
Instead the following should be used:php Syntax (Toggle Plain Text)
//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; }
php Syntax (Toggle Plain Text)
//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; } }
Last edited by kkeith29; May 8th, 2009 at 6:53 am.
•
•
•
•
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 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)
foreach($results as $result) {}
If $results is not an Array, it will give an error.
PHP Syntax (Toggle Plain Text)
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)
if (is_array($results)) { foreach($results as $result) {} }
I hardly see this used however. Instead is seems the common alternative used is:
PHP Syntax (Toggle Plain Text)
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.
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Other Threads in the PHP Forum
- Previous Thread: fopen problem
- Next Thread: Problem With My Website Design
| Thread Tools | Search this Thread |
ajax apache api array basics beginner binary broken cakephp checkbox class cms code codingproblem combobox cron curl database date display domain dynamic echo email error file files folder form format forms function functions google href htaccess html image include insert interactive ip java javascript joomla js limit link load login mail malfunctioning menu mlm mobile multiple mysql nodes oop outofmemmory paging parse paypal pdf php problem procedure query radio ram random recursion reference remote return script search server sessions sms source space sql syntax system table tutorial unset up-to-date update upload url validation validator variable video web webapplications websitecontactform youtube





