1. Home
  2. Computing & Technology
  3. PHP / MySQL

Pagination of MySQL Query Results

By Angela Bradley, About.com

4 of 4

Query and Results Explained

The first thing we do is re-run our query from earlier, only with one slight change. This time we are including the $max variable to limit the query results to those that belong on our current page. After your query you would display your results as normal (using any formatting you wish.)

After the results are displayed, we let the user know what page they are currently on, and the total number of pages that exist. This is not necessary but it is nice information for the user to know.

Then we generate the navigation. We assume that if you are on the first page, then you don't need a link to the first page. And as it is the first result, no previous page exists. So we check (if ($pagenum == 1) ) to see if we are on page one. If we are, then nothing happens. If we aren't on page one, then we use PHP_SELF and the page numbers to generate links to both the first page, and the the previous page.

We do almost the exact same thing to generate the links on the other side, however this time we are checking to make sure we aren't on the last page. If we are, then we don't need a link to the last page, nor does a next page exist.

Download the entire script here.

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. Pagination - PHP Pagination - PHP MySQL Pagination

©2009 About.com, a part of The New York Times Company.

All rights reserved.