tags:

views:

79

answers:

2

Hi to all, 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.

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

+2  A: 

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

Cheers,
Eric

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

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:

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.

Eric
Sounds like he is using it correctly. Hes using LIMIT 5,10 thus getting rows from 5 to 14. Hes saying: "and when we use limit 5,10 we get 10 records from 5"
ChrisAD
You were right. The question changed since I answered it, so I answered the real question.
Eric
A: 

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.

acrosman
Thank you Acrosman . Yes you are right we can read the index using counter. But I have fetched the data in random order so it in not applicable for this one. So how can i read that index in this case. Thanks again
It's not clear to me what you're goal is. If you just need the index for display purposes, what does the order mater? Are you trying to have an ID that re-numbers when you delete rows so you have a sequential count of the records?
acrosman