简体   繁体   English

PHP和MySQL - 显示数据库中最后十个条目的最有效方法?

[英]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.

相关问题 用PHP多次查询mysql数据库的最有效方法是什么? - What is the most efficient way to query a mysql database multiple times with PHP? PHP Mysql - 在一段时间内收集数据并更新数据库的最有效方法? - PHP Mysql - Most efficient way to gather data in periods of time and update database? PHP / MySQL / PDO库 - 从数据库中分发信息? 什么是最有效的方式? - PHP/MySQL/PDO Library - Distributing Information Out Of A Database? What's the most efficient way? 在php页面的MySQL数据库表中显示条目 - Display entries in MySQL database table in php page 读取文件第一行和最后一行的最有效的PHP方法是什么? - What is the most efficient PHP way to read first and last line of a file? 什么是在MySQL数据库中保存数据的最有效方法 - What's the most efficient way to save data in MySQL Database 在MYSQL数据库中存储和查询hastags的最有效方法是什么 - What is the most efficient way of storing and querying for hastags in a MYSQL database php&mysql:检查数据库大数组的最有效方法 - php & mysql: most efficient method to check large array against database 从php到mysql存储大整数的最节省空间的方法? - Most space efficient way to store large integer from php to mysql? 在多个PHP文件中连接MySQL的最有效方法 - Most efficient way to connect to MySQL in multiple PHP files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM