我正在开发自己的论坛,除了我尝试按日期时间排序外,一切都运行良好。我的表名是正确的,所有字段名也是正确的!当我将 ORDER BY date_time 添加到查询时出现 PHP 错误,我不明白为什么,因为它在网站的其他地方工作得很好。代码和错误如下:
// 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);。
最佳答案
您的 ORDER BY 子句需要在 WHERE 子句之后:
SELECT * FROM forum_replb WHERE post_id='$disc_id' ORDER BY date_time
另请注意,mysql_ 系列函数已弃用。您应该使用 mysqli_ 函数,或者可能更好的是 PDO。
关于PHP 和 MySQL ORDER BY date_time 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071679/