0

I looked at the other answers to problems similar to mine but cant seem to solve this.

Here is the code.

  $connection = mysql_connect("localhost","root","starwars");
  $conn = mysql_select_db("project", $connection);

        // This code assumes $itemID is set to that of 
   // the item that was just rated. 
   // Get all of the user's rating pairs
   $sql = "SELECT DISTINCT r.itemID, r2.ratingValue - r.ratingValue 
               as rating_difference
               FROM rating r, rating r2
               WHERE r.userID=$userID AND 
                       r2.itemID=$itemID AND 
                       r2.userID=$userID;";

   $db_result = mysql_query($sql, $conn);
   echo "The result is {$db_result}";
   $num_rows = mysql_num_rows($db_result)or die('Cannot Execute:'. mysql_error());

The error being displayed is:

Warning: mysql_query() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\recomender\ratingfiles\class.rating.php on line 177

Line 177 is

   $db_result = mysql_query($sql, $conn);

And if I echo $conn it gives the value of "1" which I thought was equal to true, thus boolean, any ideas?

Kami
  • 19,134
  • 4
  • 51
  • 63

4 Answers4

1

This probably means $conn is false, meaning it didnt get setup correctly. You may want to check how you have set that up and ensure the database connection details are correct and the server this is running on can access the database server.

Take a look at the return values on this page:

http://php.net/manual/en/function.mysql-connect.php

cowls
  • 24,013
  • 8
  • 48
  • 78
  • I have checked the database connection and that all works fine, and i know that $conn is carrying the value 1, is there anyway it can be losing that value between being set and being used in those few lines? – Daniel-John Edward Crookes Jan 18 '13 at 10:36
  • Post more code so we can see how youre setting it up, and make sure youre not changing it after it is set up – cowls Jan 18 '13 at 10:36
1

Pass $connection as the second parameter, not $conn.

You assign the result of mysql_select_db to $conn, and mysql_select_db returns true or false, not a connection resource.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
0

change first 2 lines to

$conn = mysql_connect("localhost","root","starwars");
mysql_select_db("project", $conn);

or, better, just refrain from using connection variable at all

mysql_connect("localhost","root","starwars");
mysql_select_db("project");
...
$db_result = mysql_query($sql);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1
$db_result = mysql_query($sql, $connection);

Second argument to mysql_connect must have

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

http://php.net/manual/en/function.mysql-query.php

Sahal
  • 4,046
  • 15
  • 42
  • 68