[英]PHP & MySQL - Most efficient way to display last ten entries in a database?
So I have a database, and I would like to display the last ten entries. 所以我有一个数据库,我想显示最后十个条目。
So far I have tried several ways but run into a couple of issues. 到目前为止,我已尝试了几种方法,但遇到了几个问题。 I have tried using a for-loop starting at the row count for the database and linking that to the ID, but then of course should I delete an entry the iterations will have data missing.
我已经尝试使用for循环从数据库的行计数开始并将其链接到ID,但当然我应该删除一个条目,迭代将丢失数据。 For example if I have deleted lots of entries and have the first entry start at id 20 then if there are only 10 rows it will look to display 10 to 1, which don't exist.
例如,如果我删除了大量条目并且第一个条目从id 20开始,那么如果只有10行,它将看起来显示10到1,这不存在。
Any thoughts? 有什么想法吗?
您可以尝试,具体取决于您的实施,
SELECT * FROM `table_name` WHERE conditions ORDER BY `id` DESC LIMIT 10;
Try this : 尝试这个 :
mysql_connect("localhost","usernm","pwd");
mysql_select_db("database");
$rs=mysql_query("SELECT id,col1,col2,coln FROM table_name WHERE condt ORDER BY id DESC LIMIT 10") or die(mysql_error());
while($row= mysql_fetch_row($rs))
{
print_r($row);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.