简体   繁体   English

如何获得Mysql数据库表中的最后十行?

[英]How can I get the last ten rows in a Mysql database table?

I use this 我用这个

$query = "SELECT * FROM info ORDER BY id DESC limit 10";
$result = @mysql_query( $query );
 $row = mysql_fetch_array($result);
 print_r($row);

but it gets just the last row 但它只是最后一行

mysql_fetch_array does not fetch an array of rows. mysql_fetch_array不获取行数组。

It fetches an array of columns from a single row. 它从单行中获取列的数组。

To get all rows, you have to run it in a loop: 要获取所有行,必须循环运行:

$query = "SELECT * FROM info ORDER BY id DESC limit 10";
$result = @mysql_query( $query );
while ($row = mysql_fetch_array($result))
        print_r($row);

The query is correct. 该查询是正确的。 If you seem to be only getting one row, it's a factor external to the query causing it: either you only have one row in the table, or your application logic is hosed so that it looks like you only have one row. 如果您似乎只得到一行,那么它是导致查询的外部因素:表中只有一行,或者应用程序逻辑被压缩,看起来好像只有一行。

Edit: Yeah, now that you've posted your code, we can see that it's that your application logic is hosed. 编辑:是的,现在您已经发布了代码,我们可以看到,这是您的应用程序逻辑被压缩的原因。 Try this: 尝试这个:

$result = mysql_query($query);
$rows = array();
while($row = mysql_fetch_array($result))
    $rows[] = $row;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM