Helpful Information
 
 
Category: Database Management
Break up query into pages using PHP from MsSQL

I'm trying to split results into different pages and number the pages accordingly. I'm using PHP and accessing a MsSQL database. I've been able to build a fairly intelligent page numbering system which knows which rows it should pull (at least by numbering each row numerically) but I don't know how to construct a SQL query to pull 10 results starting at a certain number. I know MySQL has a function called offset. This combined with the limit function would be great! But MsSQL doesn't seem to work in the same way. Here's what I have so far:

SELECT count(id) FROM members WHERE bla bla bla

then load that into a variable called $total.

SELECT TOP 10 * FROM members WHERE bla bla bla

TOP is the function to only pull a certain number of records. (I had to figure that out the hard way!)

Any hints or suggestions would be greatly appreciated!!!

Thanks,
Dean :cool:

you need to use Top keyword in the sql statement to pull a certain number of record



SELECT TOP 10 * FROM members WHERE bla bla bla

here is a suggestion from me:


initialize a variable called offset and create a limit value when you pass that to sql statement.


if(empty($offset)){
$offset=0;
}

$limit=$offset+10;

now you pass this limit to sql

SELECT TOP $limit * FROM members WHERE bla bla bla


when you loop through the data ,it should start from offset value to limit value.

you need to increment the offset value on each page link (ie, 10,20...)

i hope you might have got what i'm trying to explain above.

Does anyone have any further suggestions on doing this?

The TOP keyword really doesn't work in the example above because you can only specify the first N number of rows. I'd like to duplicate what can be done with the MySQL LIMIT function.

TIA,

Don

select top 10 cl.*
from dbo.Tbl_CurrentLocation cl
where cl.XREF not in (select top 0 l.xref from dbo.Tbl_CurrentLocation l)

select top 10 cl.*
from dbo.Tbl_CurrentLocation cl
where cl.XREF not in (select top 10 l.xref from dbo.Tbl_CurrentLocation l)

select top 10 cl.*
from dbo.Tbl_CurrentLocation cl
where cl.XREF not in (select top 20 l.xref from dbo.Tbl_CurrentLocation l)










privacy (GDPR)