[英]How to iterate through a table from last row to first?
My table: 我的桌子:
-----------------------
| id: | data: |
|---------------------|
| 1 | a |
| 20 | b |
| 546 | c |
-----------------------
I want to store the data in an array from last inserted row (id is auto incremented) to first. 我想将数据从最后插入的行(id自动递增)到第一个存储在数组中。
$sth = $dbh->query('SELECT * FROM mytable');
$data = array();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
array_push($data, $row);
}
return $data;
What can I add to the query in order to loop through the table from bottom to top? 我可以在查询中添加什么,以便从下到上遍历该表?
I tried variations of ORDER BY
but they didn't work, I'm missing something. 我尝试了
ORDER BY
变体,但没有用,我错过了一些东西。
Get records by descending order 按降序获取记录
use following query 使用以下查询
SELECT * FROM mytable order by id desc
where id is the auto increment id of your table 其中id是表格的自动增量ID
Change MySQL statement to be 将MySQL语句更改为
SELECT * FROM 'mytable' ORDER BY 'id' DESC
or reverse the array using PHPs reverse array function 或使用PHP的反向数组功能反向数组
return array_reverse($data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.