dongyunqin7307 2014-04-08 08:50
浏览 61
已采纳

MySQL查询中的SORT BY不会更改查询结果数组中的顺序

This MySQL query

SELECT * FROM notes WHERE school_code = 'brisch' ORDER BY ID DESC

works in the MySQL terminal (showing records in descending ID order where they held in the table in ascending order) but when I use it in a PHP script as:

$query = "SELECT * FROM notes WHERE school_code = '$skcode' ORDER BY ID DESC";
$nresult = mysql_query($query);
$nnum = mysql_numrows($nresult);

And then output the rows using:

$i=0;
while ($i < $nnum) 
{
   // take each row from the array $nresult
   $i++;
}

It displays the rows in the table order i.e. by ascending ID I thought if they pulled from the table in descending order they would be stored in the array in that same order.

Am I wrong in thinking that?

  • 写回答

2条回答 默认 最新

  • doumeikuan6834 2014-04-08 09:58
    关注

    The error is most likely how you iterate through the results. Replace while loop with mysql_fetch_assoc() and it should work:

    while ($row = mysql_fetch_assoc($nresult)) {
    
        echo $row['id'];
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决