![](/img/trans.png)
[英]mysql_num_rows(): supplied argument is not a valid MySQL result resource
[英]mysql_num_rows(): supplied argument is not a valid MySQL result resource
如果選擇得到一行,我需要得到真實的結果。 但是我什至無法回顯行數。
我得到mysql_num_rows(): supplied argument is not a valid MySQL result resource
我該怎么做?
$result="SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1";
$num_rows = mysql_num_rows(resource $result);
echo $num_rows;
在嘗試從執行中獲取任何結果之前,必須使用mysql_query()
執行SQL查詢。
因此,基本上,您的代碼應如下所示:
// This is a QUERY, not a RESULT
$query = "SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1";
$result = mysql_query($query); // execute query => get a resource
if ($result) {
// query has been executed without error
$num_rows = mysql_num_rows($result); // Use the result of the query's execution
echo $num_rows;
}
else {
// There has been an error ; deal with it
// for debugging, displaying the error message is OK :
echo mysql_error();
}
您必須先執行SQL查詢,然后才能檢索結果集中的行數:
$mysql_result = mysql_query($result); // Do this first, then you can
$num_rows = mysql_num_rows( $mysql_result);
這是一種更清潔的方法
$st = "SELECT COUNT(1) FoundCount FROM Users WHERE User='$user' AND Password='$hash'";
$result = mysql_query( $st );
$row = mysql_fetch_assoc( $result );
$rowfound = $row['FoundCount'];
試試看 !!!
嘗試:
$result = mysql_query("SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1");
另外,執行查詢后,請檢查是否有任何錯誤。 在這種情況下, $result
不是資源(實際上是FALSE
)。 要檢索錯誤詳細信息,請調用mysql_error()
:
$query = "SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1";
$result = mysql_query($query);
if ($result === FALSE)
die("Error in query $query: " . mysql_error());
這是你的責任,但是,這些錯誤處理例程不讓它投入生產的代碼,因為這會傳播你的數據庫的布局。 另外,檢查插入查詢中的變量是否正確轉義(為此使用mysql_real_escape_string()
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.