我正在開發我自己的論壇,除了嘗試使用ORDER BY date_time時,所有的工作都是完美的。我的表名是正確的,所有的字段名稱都是正確的!當我將ORDER BY date_time添加到查詢中時發生PHP錯誤,我無法理解爲什麼,因爲它在網站上的其他地方完全正常工作。碼和誤差都低於:PHP和MySQL ORDER BY date_time錯誤
// query responses
$sqlresponses = mysql_query("SELECT * FROM forum_replb ORDER BY date_time WHERE post_id='$disc_id'");
$responseList = "";
$numRows = mysql_num_rows($sqlresponses);
if ($numRows < 1) {
$responseList = "There are currently no responses to this discussion/post yet! Add one above.";
} else {
while($row = mysql_fetch_array($sqlresponses)){
$response_author_id = $row["author_id"];
$reply_body = $row["reply_body"];
$date_time = $row["date_time"];
$date_time = strftime("%b %d, %Y", strtotime($date_time));
$responseList .= '<div id="sub_response_module"><p>' . $reply_body . ' -
<a href="../profile.php?id=' . $response_author_id . '">' . $response_author_id . '</a>
| <i>' . $date_time . '</i></p></div>';
}
}
錯誤:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/public_html/lb/forum/post.php on line 49
只是爲了澄清,線49是$ = numRows行mysql_num_rows($ sqlresponses);.
使用['mysql_error'](http://php.net/manual/en/function.mysql-error.php)獲取實際的錯誤信息。如果你的查詢失敗了,'mysql_query'將返回'false'而不是'resource'。這裏的問題是你忽略了這種情況,無論如何都要在$ sqlresoonse上調用'mysql_num_rows'。有關示例,請參閱手冊。 – Basti 2012-04-09 10:10:32