从mysql_fetch_assoc()以相反的顺序回显 [英] echo in reverse order from mysql_fetch_assoc()

查看:110
本文介绍了从mysql_fetch_assoc()以相反的顺序回显的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这就是窍门. 在查询中,我从名为messages的表中获得了正确的结果. (它以相反的顺序获取按插入时间排序的最后10条消息),这是查询:

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

后来,这些消息通过ajax通过

打印在div内

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

但是打印结果我只想按相反的顺序打印. 提示:如果我在ORDER BY子句中使用ASC,则不会得到最后插入的消息.

这可能在php里面做吗?

解决方案

存储您的数据&比使用 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'];
}

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?

解决方案

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'];
}

这篇关于从mysql_fetch_assoc()以相反的顺序回显的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆