-1

I have a text field called user and a submit button which displays a Json string with details of a user in the database with that id. I have the following code:

$UserID = mysql_real_escape_string($_POST['User']);

$UserCoords_Query  = "Select Accuracy, Longitude, Latitude, Timestamp 
                      FROM UserDetails
                      WHERE UserId =".$UserID;

$UserCoords_result = mysql_query($UserCoords_Query);

if (mysql_num_rows($UserCoords_result) == 0) {
    echo "There are no users with an id of ". $UserID;
}

But I am getting an error of:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/roseanne/public_html/display_Coords.php on line 29.

I had recently switched the type of userID in my database from int to text. It worked as an int but not since the change. Can anyone see the quotes problem?

Matt Cain
  • 5,468
  • 3
  • 34
  • 45
user2363025
  • 5,917
  • 16
  • 42
  • 87

3 Answers3

0

Timestamp is reserved word in mysql, use ` around it

$UserCoords_Query  = "Select `Accuracy`, `Longitude`, `Latitude`, `Timestamp`
                  FROM UserDetails
                  WHERE UserId = '".$UserID."'";
Yogesh Suthar
  • 30,136
  • 18
  • 69
  • 98
0

mysql_query returns false on failure, so you have to check for it. In your case it returned false - your query failed, but you still passed this false value into function mysql_num_rows, which raised an error.

Also, you should use mysqli_* as mysql_* is deprecated.

$UserID = mysql_real_escape_string($_POST['User']);

$UserCoords_Query  = "Select `Accuracy`, `Longitude`, `Latitude`, `Timestamp` 
                      FROM UserDetails
                      WHERE UserId =".$UserID;

$UserCoords_result = mysql_query($UserCoords_Query);

if ($UserCoords_result && mysql_num_rows($UserCoords_result) >= 1) {
  //Fetch results
} else {
  echo "There are no users with an id of ". $UserID;
}
sybear
  • 7,789
  • 1
  • 21
  • 38
  • Got it sorted, I needed to change to this: $UserCoords_Query = "Select Accuracy, Longitude, Latitude, Timestamp FROM UserDetails WHERE UserId ='".$UserID."'"; – user2363025 May 09 '13 at 10:44
0

$UserCoords_result is not being set as a valid resource for the function mysql_num_rows(). This may be due to mysql_query trying to run without you first creating a connection to your database. I would need to see more code to be sure.

Also while you are developing this, I would suggest you move to mysqli or DBO as mysql_ functions are being depreciated as of PHP5.5

:)

lukeocodes
  • 1,155
  • 1
  • 16
  • 31
  • I have made the code for the connection to the database and the query worked before I changed the data type. the $UserId in the query needs to be in quotes I think but I don't know how to do that syntax. Thanks for the suggestion! :) – user2363025 May 09 '13 at 10:39