查询失败您的SQL语法中有错误;靠近'DESC'的第1行

问题描述 投票:0回答:2

我尝试了很多解决方案,没有一个有效。如果我注释掉DESC查询页面加载正常但没有注释,如果我把查询放回去,我得到错误。我无法看清我做错了什么,并坚持到这一点,直到我解决它。任何帮助将非常感激。

    <!-- Posted Comments -->

<?php //post comments query
$query = "SELECT * FROM comments WHERE comment_post_id = {$the_post_id} ";
$query .= "AND comment_status = 'Approved' ORDER BY comment_id = DESC ";
//$query .= "ORDER BY comment_id = DESC ";
$select_comment_query = mysqli_query($connection, $query);
if(!$select_comment_query) {

    die('Query Failed' . mysqli_error($connection));

}
while ($row = mysqli_fetch_assoc($select_comment_query)) {
$comment_date = $row['comment_date'];
$comment_content = $row['comment_content'];
$comment_author = $row['comment_author'];

?>

<!-- Comment -->
<div class="media">
    <a class="pull-left" href="#">
        <img class="media-object" src="http://placehold.it/64x64" alt="">
    </a>
    <div class="media-body">
        <h4 class="media-heading"><?php echo $comment_author; ?>
            <small><?php echo $comment_date; ?></small>
        </h4>
        <?php echo $comment_content; ?>
    </div>
</div>

<?php } ?>

</div>
mysql sql
2个回答
2
投票

我相信=是不必要的。也许你打算:

ORDER BY comment_id DESC

这命令comment_id降序的结果集。

或者,如果您想要首先出现特定的comment_id

ORDER BY comment_id = ? DESC

哪里?是你想要的价值。


0
投票

排序依据列名称使用默认值ASC。

如果要按降序对数据进行排序,则在列名称后添加空格并添加ASC或desc以按升序排序

Order by column_name ASC
Or
Order by column_name

用于按降序排序

Order by column_name DESC
© www.soinside.com 2019 - 2024. All rights reserved.