Why do I keep getting this error message?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Horhay5150
    New Member
    • Feb 2010
    • 3

    Why do I keep getting this error message?

    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.

    Code:
    <?php
    
    
    $input = $_POST['Rating'];
    
    
    //connect to database
    $link = mysql_connect("localhost", "name", "pword"); 
    $db = mysql_select_db("test", $link);
    
    
    $query = "SELECT RatingID FROM rating WHERE rating.Rating ='$input'";
    $result = mysql_query($query, $db) or die(mysql_error()); 
    
    $row = mysql_fetch_array($result);
            echo "Rating ID - ". $row['RatingID'];
            
    
    
    ?>
    Last edited by Atli; Feb 18 '10, 08:47 AM. Reason: Please use [code] tags!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    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:
    [code=php]// You can do:
    $link = mysql_connect(' host', 'usr', 'pwd');
    if($link === false) {
    die("MySQL connection could not be established: " . mysql_error());
    }

    // Or simply:
    $link = mysql_connect(' host', 'usr', 'pwd') or die(mysql_error ());[/code]

    Comment

    • Horhay5150
      New Member
      • Feb 2010
      • 3

      #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.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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?

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          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.

          [code=php]// This
          mysql_query($qu ery, $db)

          // Should be
          mysql_query($qu ery, $link)[/code]

          Comment

          • Horhay5150
            New Member
            • Feb 2010
            • 3

            #6
            THANK YOU. That did the trick. Appreciate the help.

            Comment

            Working...