开发者

Getting the last 7 rows from MySQL in PHP

I'm trying to get the last 7 records from a table whose records were entered by the user

Here is my query:

$database->setQuery("SELECT * FROM #__mytable WHERE (user_id = '$uid')");
$dberr="";
if (!$database->query()) {
    $dbe开发者_如何学Pythonrr = $database->getErrorMsg();
}

if(!$dberr==""){
   echo($dberr."<br>");
}else{
   $rows = $database->loadObjectList();

How do I cycle thru the $rows to get the last 7?


You don't:

SELECT * FROM ... WHERE ... ORDER BY user_id DESC LIMIT 7


SELECT  *
FROM    #__mytable
WHERE  user_id = '$uid'
ORDER BY
       entered_date DESC
LIMIT 7

To get them in ascending order, use:

SELECT  *
FROM    (
        SELECT  *
        FROM    #__mytable
        WHERE  user_id = '$uid'
        ORDER BY
               entered_date DESC
        LIMIT 7
        ) q
ORDER BY
        entered_date


SELECT * FROM #__mytable WHERE (user_id = '$uid') ORDER BY id DESC LIMIT 0,7
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜