---
title: "Pagination in PHP"  
description: "As your database grows, showing all the results of a query on a single page is no longer practical that’s why pagination is used in PHP. You can displ"  
author: "Anonymous User"  
published: 2011-11-15  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/265/pagination-in-php  
category: "php"  
tags: ["php"]  
reading_time: 1 minute  

---

# Pagination in PHP

[As your](https://answers.mindstick.com/qa/95660/how-do-you-remove-firefox-as-your-default-browser) [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) grows, showing all the [results](https://yourviews.mindstick.com/story/4497/usage-of-baking-soda-magical-results) of a [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) on a single page is no longer [practical](https://answers.mindstick.com/blog/54/minimal-apis-in-dot-net-a-practical-guide) that’s why [pagination](https://www.mindstick.com/blog/302239/what-is-pagination-and-best-practices) is used in PHP. You can [display your](https://answers.mindstick.com/qa/96307/how-to-connect-your-spotify-account-to-your-tinder-profile-to-display-your-music-taste) results over a number of pages, each [linked](https://answers.mindstick.com/qa/104773/how-to-create-a-linked-list) to the next, and to allow your users to browse [your content](https://yourviews.mindstick.com/view/87465/how-to-enhance-your-content-visibility-10-tips) in bite sized pieces.\

##### Code:

```
<?php        // connect with database           mysql_connect("localhost", "root", "root");       // select database name        mysql_select_db("tempdatabase");        /* We will bypass the database connection code ... */        // select all data from your table        $sqlQuery = "select count(*) from tempdatabase.tbltemp";        // store result of query        $result = mysql_query($sqlQuery);        $totalRows = mysql_num_rows($result);        $pager_options = array(        'mode'       => 'Sliding',        'perPage'    => 10,        'delta'      => 4,        'totalItems' => $totalRows,        );        $pager = Pager::factory($pager_options);        // return the links of pages         echo $pager->links;

?>
```

The above code will return the following pagination links.

## 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 » [10]

Assuming the page where the above code is included is named ‘MydataPage.php’; the pager links will have a url like follows:

MydataPage.php?pageID=n

Where n is the page number.

---

Original Source: https://www.mindstick.com/blog/265/pagination-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
