PHP - SELECT * FROM ...但首先查看数据库中的最后一个元素
问题描述:
Hey, first of all thanks for helping me out with this (probably stupid) question :) I did my research but can't find any solutions on google.
Is it possible to start viewing the last element in my database?
for example:
DATABASE (comments):
name | comment
jan | Test
bert | Test2
sam | Test3
PHP:
$sql="SELECT * FROM comments";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
echo $rows['name'];
echo $rows['comment'];
}
THE RESULT:
jan Test
bert Test2
sam Test3
MY PURPOSE:
sam Test3
bert Test2
jan Test
Thanks in advance for your help :)
嘿,首先感谢帮助我解决这个(可能是愚蠢的)问题:) 我做了我的研究 但是在谷歌上找不到任何解决方案。 p>
是否可以开始查看数据库中的最后一个元素? p>
例如: p>
数据库(注释): p>
name | 评论
jan | 测试
bert | Test2
sam | Test3
code> pre>
PHP: p>
$ sql =“SELECT * FROM comments”;
$ result = mysql_query ($ sql);
而($ rows = mysql_fetch_array($ result)){
echo $ rows ['name'];
echo $ rows ['comment'];
}
pre>
结果: p>
jan Test
bert Test2
sam Test3
code> pre> \ n
我的目的: p>
sam Test3
bert Test2
jan Test
code> pre>
谢谢 提前寻求帮助:) p>
div>
答
Change your mysql query to sort the result set how you want. In your case it is probably something like
SELECT * FROM comments ORDER BY id DESC
答
You should add either an id to your table, or at least a timestamp that contains the time and date of insertion of your data row.
Then :
SELECT * FROM comments ORDER BY timestamp DESC