<Marc Qualie/>

Pagination with PHP and MySQL

This is a quick tutorial on how to code basic pagination using a combination of PHP and MySQL. Pagination is basically splitting a list of content between multiple pages for easier navigation and less scrolling on a website. To start with you are going to need a few variables.

$page_number = intval($_GET['p']) ? $_GET['p'] : 1; // Current page you are viewing
$page_limit = 10; // Items per page
$page_offset = ($page_number - 1) * $page_limit; // Where to start MySQL reading

Once you have the above variables you can then call the data from MySQL, like so:

$result = mysql_query("SELECT * FROM table LIMIT {$page_offset},{$page_limit}");

Now to create the actual links (1 – 10):

for ($i = 1; $i < 10; $i++) {
  echo "<a href="?p={$i}\">{$i}</a>";
  if ($i != 10) {
    echo " | ";

There you have it, extremely basic pagination.

If you have any questions about this post, or anything else, you can get in touch on Twitter or browse my code on Github.