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

时间:2022-09-25 15:39:43

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

但它只是最后一排

2 个解决方案

#1


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);

#2


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;

#1


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);

#2


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;