一尘不染

按ID DESC排序

sql

我只是想按ID对评论进行排序,但是这样做并不幸运。无法弄清楚该怎么办,因为这使我感到困惑:articleid='" . mysql_real_escape_string($_GET['id']) . "'

你们碰巧知道我如何通过DESC中的ID来订购评论吗?谢谢!

<?php

$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'"); $comments = mysql_num_rows($amount_get);

$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'");


if (mysql_num_rows($grab)==0) {

    echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
}

    while($row = mysql_fetch_array($grab)){

    ?>

阅读 187

收藏
2021-03-08

共1个答案

一尘不染

首先,您要进行SELECT两次相同的操作。这是不必要的,因为您可以计算行数并从单个查询中获取数据。除此之外,commentid用注释表的唯一ID替换您,即可设置好。替换DESCASC以颠倒排序顺序。

<?php
    $grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY commentid DESC");
    $comments = mysql_num_rows($grab);

    if (mysql_num_rows($grab)==0) {
        echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
    }

    while($row = mysql_fetch_array($grab)){

?>
2021-03-08