2

我只是想通过 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)){

    ?>
4

7 回答 7

8

首先你做了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)){

?>
于 2012-11-29T07:46:28.503 回答
1

添加ORDER BY子句

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

您的查询易受攻击SQL Injection,请阅读下面的文章以保护它,

于 2012-11-29T07:40:39.987 回答
0

试试这个

$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' order by articleid desc");
于 2012-11-29T07:41:33.567 回答
0

我认为您的评论表应该是一列id,所以:

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

这是一个 sql 的东西来排序,而不是 php,所以你只需要修改你的 sql 语句。

于 2012-11-29T07:44:57.033 回答
0
$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");
于 2012-11-29T07:45:23.303 回答
0

三个建议:

1)在字符串变量中完成“选择”语句(如下所示;它使调试更容易)

2)考虑使用准备好的语句而不是原始的“选择”(或更新或删除!)。

它可以帮助性能。但它使您的 PHP更加安全!

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);
  ...

这是取代旧的 mysql_query() 语法的 mySQLi 和 PDI API 的一个很好的链接:

这是关于准备好的陈述的一个很好的链接:

于 2012-11-29T07:45:53.993 回答
0

您可以在查询中使用 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
}

    ?>
于 2012-11-29T07:56:35.710 回答