MySQL显示倒序的结果?

时间:2011-09-06 08:29:39

标签: php mysql

我需要获得最后5个结果,这就是为什么我通过Date DESC订购它们但我需要显示从旧到新的结果。我怎么能这样做?

$data = mysql_query("SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC LIMIT 5"); 

while($row = mysql_fetch_array( $data )) 
    { 
    print "<img class='badge' title='" . $row['Site'] . "' src='http://getfavicon.appspot.com/http://" . $row['Site'] . "?defaulticon=1pxgif' />";
    }

3 个答案:

答案 0 :(得分:2)

$results = array();
while($row = mysql_fetch_array($data)) {
    $results[] = $row;
}

$results = array_reverse($results);

foreach ($results as $row) {
    echo $row['Site']; // etc
}

手动链接:http://php.net/function.array-reverse.php

答案 1 :(得分:0)

您实际上可以在同一查询中再次订购。

我假设你有一个自动递增ID(在这个例子中我称之为'EntryId'),然后你希望得到你需要的东西?

SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC, EntryId ASC LIMIT 5

答案 2 :(得分:0)

$data = mysql_query("
    SELECT * FROM (
        SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC LIMIT 5) 
    t ORDER BY Date
");

此外: 请使用mysql_real_escape_string(http://at2.php.net/manual/en/function.mysql-real-escape-string.php)作为变量$ user。根据你从哪里得到它可能是通过sql注入的安全漏洞。