By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Why do I keep getting this error message?

P: 3
I am admittedly a noob to PHP and mySQL, Have been running through some tutorials. Whenever I run the following script I get this error message: mysql_query() expects parameter 2 to be resource, boolean given.

Any help would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. <?php
  4. $input = $_POST['Rating'];
  7. //connect to database
  8. $link = mysql_connect("localhost", "name", "pword"); 
  9. $db = mysql_select_db("test", $link);
  12. $query = "SELECT RatingID FROM rating WHERE rating.Rating ='$input'";
  13. $result = mysql_query($query, $db) or die(mysql_error()); 
  15. $row = mysql_fetch_array($result);
  16.         echo "Rating ID - ". $row['RatingID'];
  20. ?>
Feb 18 '10 #1
Share this Question
Share on Google+
5 Replies

Expert 5K+
P: 5,058

That means that your mysql_connect call is failing. When it is successful, it returns a resource that you can pass into the mysql_query function. However, when it is unsuccessful, it returns FALSE which, when passed into the mysql_query function, generates this error.

You should verify that the mysql_connect call was successful before trying to pass the $link to the mysql_query function.

For example:
Expand|Select|Wrap|Line Numbers
  1. // You can do:
  2. $link = mysql_connect('host', 'usr', 'pwd');
  3. if($link === false) {
  4.     die("MySQL connection could not be established: " . mysql_error());
  5. }
  7. // Or simply:
  8. $link = mysql_connect('host', 'usr', 'pwd') or die(mysql_error());
Feb 18 '10 #2

P: 3
added the suggested code, error message is still the same. For some reason mysql_Query isnt happy with $db, as far as i have been able to tell though my syntax on both connect and db selection is correct, i'm stumped.
Feb 18 '10 #3

Expert 5K+
P: 5,058
added the suggested code, error message is still the same.
That can't be possible. If you added either of those and the connection fails, the code should be stopped before it reached the query call.

Can you show us how you modified the code?
Feb 18 '10 #4

Expert 5K+
P: 5,058
Ahhh ok. I see now. Never mind my previous post.

You are using the wrong variable in your mysql_query call. You should be using the result of the mysql_connect call, not the mysql_select_db call.

Expand|Select|Wrap|Line Numbers
  1. // This
  2. mysql_query($query, $db)
  4. // Should be
  5. mysql_query($query, $link)
Feb 18 '10 #5

P: 3
THANK YOU. That did the trick. Appreciate the help.
Feb 18 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.