为什么查询不按降序显示结果?

时间:2014-03-08 15:12:44

标签: php mysql

显示帖子的代码是:

for($i=0; $i<$count; $i++)
                       {
                        $result1 = mysqli_query($con1,"SELECT pid_1,title,ts from post2                 
                                  where pid_1='$pid[$i]' order by pid_1 desc");
                        while($row = mysqli_fetch_array($result1))
                         {
                              $t=$row['title'];
                          $p=$row['pid_1'];
                          $ts=$row['ts'];

                        echo $t."   ".$p."   ".$ts."<br>";

                        }}

,其中  count =帖子总数  $ pid [$ i] =要访问数组$ pid中的每个元素($ pid数组存储要选择的所有帖子的帖子ID)

即使包含“按pid_1 DESC排序”,结果也会随机出现。怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果你想要它们,你必须对pid array进行排序,或者你只是在一个查询中获取所有帖子并让查询进行排序:

$pids_str = explode("'",$pid); 
$result1 = mysqli_query($con1,"SELECT pid_1,title,ts FROM post2 WHERE pid_1 IN ('".$pids_str."') ORDER BY pid_1 DESC");
while($row = mysqli_fetch_array($result1))
{
    $t=$row['title'];
    $p=$row['pid_1'];
    $ts=$row['ts'];

    echo $t."   ".$p."   ".$ts."<br>";
}