This question already has an answer here:
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?
</div>
Timestamp
is reserved word in mysql
, use `
around it
$UserCoords_Query = "Select `Accuracy`, `Longitude`, `Latitude`, `Timestamp`
FROM UserDetails
WHERE UserId = '".$UserID."'";
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;
}
$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
:)