controling the number of records returned from a query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alip
Forum Newbie
Posts: 2
Joined: Tue Nov 04, 2003 6:12 am

controling the number of records returned from a query

Post by alip »

Hi i am looking for the best way in which to limit the number of records returned from a query on my database. say my query returns 50 records when submitted, i would like to show 10 on the first page use a next button to show the next 10 and so on. A previous page button is also required to view the last 10 etc.
Can anyone shed some light on this ?? :?:
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Which database are you using?

If you are using MySQL, I know you can use the LIMIT keyword. Such as:

Code: Select all

SELECT * FROM table WHERE something = something LIMIT 10
alip
Forum Newbie
Posts: 2
Joined: Tue Nov 04, 2003 6:12 am

Post by alip »

yeah its mysql database, i can limit the number but i also need to be able to call the next 10 with a 'next' link and same for previous.
Manix
Forum Newbie
Posts: 1
Joined: Tue Nov 04, 2003 6:55 am
Location: United Kingdom
Contact:

Post by Manix »

The proper way to limit your query is like this...

Code: Select all

SELECT * FROM $table_name WHERE $column = '$variable' LIMIT $start_from, $total_output
and without variables...

Code: Select all

SELECT * FROM table WHERE something = 'something' LIMIT 0, 25
that should work a little more efficiently!

//EDIT - Expanded on the answer!
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

so what you are saying, is you can call the query :

Code: Select all

SELECT * FROM table WHERE something = 'something' LIMIT 0, 25
and use

Code: Select all

SELECT * FROM table WHERE something = 'something' LIMIT 26, 50
and so on? if not, sorry just wondering myself on this one
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

infolock wrote:so what you are saying, is you can call the query :

Code: Select all

SELECT * FROM table WHERE something = 'something' LIMIT 0, 25
and use

Code: Select all

SELECT * FROM table WHERE something = 'something' LIMIT 26, 50
and so on? if not, sorry just wondering myself on this one
Yes you can. It's keeping track of your start and display that is the trick across multiple pages.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

awsome
Post Reply