按ID DESC排序 [英] ORDER BY id DESC

查看:321
本文介绍了按ID DESC排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I simply want to ORDER the comments by the ID, but I have no luck in doing it. Can't figure out what to do, because this is confusing me: articleid='" . mysql_real_escape_string($_GET['id']) . "'

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

Would you guys happen to know how I could go about ordering the comments by the id in DESC? thanks!

<?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)){

    ?>

推荐答案

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

First of all you're doing the same SELECT two times. That's pretty unnecessary since you can count rows and get the data from a single query. Additionally to this replace commentid with the unique id of your comment table and you're set. Replace DESC with ASC to reverse the sort order.

<?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)){

?>

这篇关于按ID DESC排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆