duancheng3342 2014-05-04 12:23
浏览 113
已采纳

从mysql_fetch_assoc()以相反的顺序回显

Ok here's the trick. In the query I'm getting the right results from a table named messages. (Its fetching the last 10 messages ordered by the time inserted in reversed order) here's the query:

$query = mysql_query("SELECT time, username, message
                      FROM messages ORDER BY time DESC LIMIT 10");

And than later those messages are printed inside div via ajax with

while ($item = mysql_fetch_assoc($query)) {
    echo $item['time']." - ".$item['username']." - ".$item['message'];
}

But the printed result I want to print them only in the reversed order. Tip: If I use ASC in the ORDER BY clause I don't get the last inserted messages.

Is this possible to do inside php?

  • 写回答

3条回答 默认 最新

  • duanhaodi4809 2014-05-04 12:26
    关注

    Store your data & than use array_reverse,

    while($row = mysql_fetch_assoc($result)){
        $items[] = $row;
    }
    
    $items = array_reverse($items ,true);
    
    foreach($items as $item){
       echo $item['time']." - ".$item['username']." - ".$item['message'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?