Display limited output from mysql query

I want to do the following:
1-The user search for certain item.
2-Server run sql search.
3-Display the output of the search by showing 25,50, or 100 output per page as per the user choice.
For number 1, and 2 I do that easily by php, but for number 3, I am not sure how to accomplish this, is server side or client to show only a selected number of output, can some guide me how to do so?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Avinash ZalaWeb ExpertCommented:
use LIMIT into the query. Like this:

$sql= 'SELECT * FROM table LIMIT 5';

will fetch only 5 first records.

$sql= 'SELECT * FROM table LIMIT 5 10';

will fetch 10 records starting from 5.


Hope this helps.
Ashraf-HassanAuthor Commented:
The query part is not difficult, but the display part is not clear to me.
Let us say the search result return x output, and the use chose to see 25 output in one page, then depending on x will be the number of pages if x is 100 then we need 4 pages when the user press next he will to the next page, or if he press let us 3 he will jump to page 3.
Shall I make a full query, and put in an array and dsiplay it dynamically, or each time I need to make a query for each page.
Is there any example for that?
Avinash ZalaWeb ExpertCommented:
Ok, You want to use the pager.

Here is the link for that.

This will display page links based on the data.


Hope this helps
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Hi, let's say you the current page is $page (starting with 1), and the requested number of records is $records.
This code should suit your needs :
// these values should be taken from GET/POST
$page = 1;
$records = 10;

// compute the SQL start number
$start = ($page-1) * $records;

// fetch the records
$res = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM table LIMIT $start, $records");

// get the total number of records in the table
list ($total) = mysql_fetch_row(mysql_unbuffered_query("SELECT FOUND_ROWS()"));

// compute the number of pages
// you will use this number to display links to other pages
$pages = (($total - 1) - (($total - 1) % $records)) / $records + 1;

// now you can fetch your records with mysql_fetch_*($res);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
use LIMIT into the query. Like this:

$sql= 'SELECT * FROM table LIMIT 5';

will fetch only 5 first records.

$sql= 'SELECT * FROM table LIMIT 5 10';

will fetch 10 records starting from 5.

Mehul Panchal
Ashraf-HassanAuthor Commented:
Thank you BenMorel, your code is very helpful
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.