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

4

3 回答 3

0

Timestamp是保留字,在它周围mysql使用`

$UserCoords_Query  = "Select `Accuracy`, `Longitude`, `Latitude`, `Timestamp`
                  FROM UserDetails
                  WHERE UserId = '".$UserID."'";
于 2013-05-09T10:31:47.707 回答
0

mysql_query失败时返回 false,因此您必须检查它。在您的情况下,它返回了false- 您的查询失败,但您仍然将此false值传递给 function mysql_num_rows,这引发了错误。

此外,您应该使用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;
}
于 2013-05-09T10:31:57.663 回答
0

$UserCoords_result未被设置为函数的有效资源mysql_num_rows()。这可能是由于 mysql_query 试图在您没有首先创建与数据库的连接的情况下运行。我需要查看更多代码才能确定。

另外,当你开发这个时,我建议你转移到 mysqli 或 DBO,因为 mysql_ 函数从 PHP5.5 开始被贬值

:)

于 2013-05-09T10:33:58.240 回答