如何从数据库中获取数据的细节?

时间:2021-04-27 17:00:16

Actually I was confused to give the title for this question. I have condition like this. When we get record from database using limit like select * from table_name limit 0,5 we get the data from 0 to 5 and when we use limit 5,10 we get 10 records from 5... Is it possible to display that particular index of the record using php.

实际上我很困惑地给出了这个问题的标题。我有这样的条件。当我们使用select * from table_name limit 0,5从数据库获取记录时,我们得到0到5的数据,当我们使用限制5,10时,我们从5获得10条记录...是否可以显示该特定索引使用php的记录。

Thanks..............

2 个解决方案

#1


You're using LIMIT incorrectly. LIMIT is START, LENGTH. Do LIMIT 5, 5 and you will get 5 records, starting at 5 (5-9).

你错误地使用了LIMIT。 LIMIT是START,LENGTH。做LIMIT 5,你将获得5条记录,从5(5-9)开始。

Cheers,
Eric

Edit: Please see the MySQL documentation on this, as well. It's certainly useful!

编辑:请参阅MySQL文档。这当然有用!

Another Edit: This question has been edited since I answered, so I'll answer again. It seems that you want to get the index of the row that you're returning, so here's a SQL statement to get that:

另一个编辑:这个问题已经编辑,因为我回答,所以我会再次回答。看来你想得到你要返回的行的索引,所以这里有一个SQL语句来得到它:

set @myStart = 5;
set @myLimit = 10;
set @i = @myStart - 1; 
select id, @i:=@i+1 as myrow from mytable limit @myStart, @myLimit

Note: I took this solution from here.

注意:我从这里开始使用此解决方案。

#2


If I'm reading your question correctly you are trying to display an index along with each record that's been retrieved from the database. I would do this using a counter in the loop I was using to display the records.

如果我正确地阅读了您的问题,您将尝试显示索引以及从数据库中检索到的每条记录。我会在我用来显示记录的循环中使用计数器来做这个。

Before the loop set the counter to the offset, and then increment it as you go along. If this is just a matter of displaying a counter, it's really very easy. It would only be slightly more complicated to USE that index for later work, although not much.

在循环之前将计数器设置为偏移量,然后在进行时递增。如果这只是显示计数器的问题,那真的很容易。使用该索引进行后期工作只会稍微复杂一些,尽管不多。

#1


You're using LIMIT incorrectly. LIMIT is START, LENGTH. Do LIMIT 5, 5 and you will get 5 records, starting at 5 (5-9).

你错误地使用了LIMIT。 LIMIT是START,LENGTH。做LIMIT 5,你将获得5条记录,从5(5-9)开始。

Cheers,
Eric

Edit: Please see the MySQL documentation on this, as well. It's certainly useful!

编辑:请参阅MySQL文档。这当然有用!

Another Edit: This question has been edited since I answered, so I'll answer again. It seems that you want to get the index of the row that you're returning, so here's a SQL statement to get that:

另一个编辑:这个问题已经编辑,因为我回答,所以我会再次回答。看来你想得到你要返回的行的索引,所以这里有一个SQL语句来得到它:

set @myStart = 5;
set @myLimit = 10;
set @i = @myStart - 1; 
select id, @i:=@i+1 as myrow from mytable limit @myStart, @myLimit

Note: I took this solution from here.

注意:我从这里开始使用此解决方案。

#2


If I'm reading your question correctly you are trying to display an index along with each record that's been retrieved from the database. I would do this using a counter in the loop I was using to display the records.

如果我正确地阅读了您的问题,您将尝试显示索引以及从数据库中检索到的每条记录。我会在我用来显示记录的循环中使用计数器来做这个。

Before the loop set the counter to the offset, and then increment it as you go along. If this is just a matter of displaying a counter, it's really very easy. It would only be slightly more complicated to USE that index for later work, although not much.

在循环之前将计数器设置为偏移量,然后在进行时递增。如果这只是显示计数器的问题,那真的很容易。使用该索引进行后期工作只会稍微复杂一些,尽管不多。