2013-05-09 91 views
-1

我有一個名爲user的文本字段和一個提交按鈕,該按鈕顯示帶有該ID的數據庫中用戶詳細信息的Json字符串。我有以下代碼:PHP報價問題

$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; 
} 

但我得到的錯誤:

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

我最近切換用戶ID的類型,我的數據庫從int到文本。它作爲一個整數工作,但沒有改變。任何人都可以看到報價問題?

+0

你確定你是連接並選擇數據庫是否正確? – Yousf 2013-05-09 10:31:17

+0

您正在使用[an **過時的**數據庫API](http://stackoverflow.com/q/12859942/19068)並應使用[現代替換](http://php.net/manual/en/ mysqlinfo.api.choosing.php)。 – Quentin 2013-05-09 10:31:40

+0

@Yousf Yep仍然連接! – user2363025 2013-05-09 10:36:03

回答

0

Timestampmysql保留字,使用`周圍

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

我不認爲這是問題,因爲當我在phpmyadmin中運行查詢時,它的工作原理。這是一個引用問題:/ – user2363025 2013-05-09 10:35:20

+0

@ user2363025這是問題,因爲你的查詢失敗。 – 2013-05-09 10:36:27

+0

@ user2363025請參閱使用引號編輯的查詢。 – 2013-05-09 10:59:44

0

mysql_query失敗時返回假的,所以你必須檢查它。在你的情況下,它返回false - 你的查詢失敗,但你仍然通過這個false值到函數mysql_num_rows,這提出了一個錯誤。

另外,您應該使用mysqli_*,因爲mysql_*已被棄用。

$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; 
} 
+0

得到它排序,我需要改變:$ UserCoords_Query =「選擇精度,經度,緯度,時間戳 FROM UserDetails WHERE UserId ='」。$ UserID。「'」; – user2363025 2013-05-09 10:44:25

0

$UserCoords_result沒有被設置爲功能mysql_num_rows()的有效資源。這可能是由於mysql_query試圖在沒有首先創建與數據庫的連接的情況下運行。我需要看到更多的代碼才能確定。

而且當你正在開發的這個問題,我建議你移動到庫MySQLi或DBO作爲mysql_職能正在貶值作爲PHP5.5

:)

+0

我已經做了連接到數據庫的代碼,並且在我更改數據類型之前查詢工作。查詢中的$ UserId需要用引號括起來,但我不知道該怎麼做。感謝您的建議! :) – user2363025 2013-05-09 10:39:40