好的,这就是技巧。
在查询中,我从名为 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 内部完成吗?
存储您的数据然后使用数组反转 http://www.php.net/manual/en/function.array-reverse.php,
while($row = mysql_fetch_assoc($result)){
$items[] = $row;
}
$items = array_reverse($items ,true);
foreach($items as $item){
echo $item['time']." - ".$item['username']." - ".$item['message'];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)