PHP 和 MySQL ORDER BY date_time 错误

标签 php mysql datetime sql-order-by

我正在开发自己的论坛,除了我尝试按日期时间排序外,一切都运行良好。我的表名是正确的,所有字段名也是正确的!当我将 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/

相关文章:

Django admin datefield-datetimefield 混淆

python-3.x - 使用python和pandas从html表中的另一种日期格式转换为Excel "Date"格式(在Excel文件中)

php - SQL SELECT 跳过前 N 个结果?

php - 如何获取文本中的所有链接(URL)?

mysql - 将 PostgreSQL 存储过程迁移到 MYSQL

mysql - 最有效的 mysql 查询为搜索列表中的每个结果检索多个类别

.net - 如何查找两个日期/DateTimePicker 之间的天数

php - 附加到 PHP 父类中的数组变量

php - 如果网页不存在,我应该返回什么标题?

mysql - 从 MySQL 数据库获取数据并将其传递给 jade 并使用 nodejs 访问它