php 按 ID DESC 排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13620961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ORDER BY id DESC
提问by TrippedStackers
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']) . "'
我只是想通过 ID 对评论进行排序,但我没有这样做。不知道该怎么做,因为这让我很困惑:articleid='" . mysql_real_escape_string($_GET['id']) . "'
Would you guys happen to know how I could go about ordering the comments by the id in DESC? thanks!
你们会碰巧知道我如何通过 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)){
?>
回答by bardiir
First of all you're doing the same SELECTtwo times. That's pretty unnecessary since you can count rows and get the data from a single query. Additionally to this replace commentidwith the unique id of your comment table and you're set. Replace DESCwith ASCto reverse the sort order.
首先,你在做同样的事情SELECT两次。这是非常不必要的,因为您可以计算行数并从单个查询中获取数据。此外,替换commentid为您的评论表的唯一 ID,您就完成了。替换DESC与ASC以反向排序。
<?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)){
?>
回答by John Woo
add ORDER BYclause
添加ORDER BY子句
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY articleid, ID DESC");
your query is vulnerable with SQL Injection, please read the article below to protect from it,
您的查询容易受到SQL Injection,请阅读下面的文章以防止它,
回答by Vamsi
try this
尝试这个
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' order by articleid desc");
回答by YvesR
I think your comments table habe a column idthen, so:
我认为你的评论表是一列id,所以:
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY id DESC");
This is a sql thing to sort it, not php, so you just have to modify your sql statement.
这是一个sql的东西来排序,而不是php,所以你只需要修改你的sql语句。
回答by som
$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY id DESC ");
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY id DESC");
回答by paulsm4
Three suggestions:
三点建议:
1) Complete your "select" statement in a string variable (as shown below; it makes debugging much easier)
1) 在字符串变量中完成“select”语句(如下所示;它使调试更容易)
2) Consider using prepared statements instead of raw "select" (or update or delete!).
2)考虑使用准备好的语句而不是原始的“选择”(或更新或删除!)。
It can help performance. But it makes your PHP muchmore secure!
它可以帮助提高性能。但它使你的PHP多更安全!
3) Consider moving away from the (deprecated) mysql_query() syntax
3) 考虑远离(不推荐使用的)mysql_query() 语法
<?php
$sql =
"SELECT * FROM comment WHERE articleid='" .
mysql_real_escape_string($_GET['id']) . "' order by articleid desc";
$amount_get = mysql_query($sql);
$comments = mysql_num_rows($amount_get);
$sql =
"SELECT * FROM comment WHERE articleid='" .
mysql_real_escape_string($_GET['id']) . "'order by articleid desc";
$grab = mysql_query($sql);
...
Here's a good link on the mySQLi and PDI APIs that supercede the old mysql_query() syntax:
这是一个关于取代旧的 mysql_query() 语法的 mySQLi 和 PDI API 的好链接:
And here's a good link on prepared statements:
这是准备好的声明的一个很好的链接:
回答by Avinesh Kumar
You can use order by clause in your query
您可以在查询中使用 order by 子句
<?php
$getarticles = array();
$getarticles = mysql_query("SELECT * FROM comment order by articleid desc");
if(empty($getarticles)){
echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
}
echo "<pre>";
print_r($getarticles);
echo "</pre>";
for($i=0;$i<count($getarticles);$i++){
//display
}
?>

