How to Create a Simple Pagination in PHP?

If you have many pages on your website and want to add a simple pagination for easy user navigation then you have come to the right place. Here, you’ll learn adding a very simple pagination to your website. This simple pagination is created in PHP/MySQLi which you can use on any of your web development project. I think there is no easy method to create pagination rather than this one which I’m sharing with you today. If you are a very beginner in PHP then you can follow our basic tutorials here:


How to Create a Pagination in PHP?

Now let’s create the pagination in PHP, first of all, you have to create a database using your “PHPMyAdmin” interface, you can find this in your online database server or localhost, give a name to your database such as “mytest” which I’ve given in the PHP script you’ll find below, after creating the database, just create a new table called “student” and add four (4) columns to insert student information such as: id, name, number and country. Insert some records to your table using the same PHPMyAdmin interface.

After you created the database & table, and also you inserted some records then copy the following code and paste it to notepad++ or any other text editor you use, save the file with the name “pagination.php”, here is the code:

<!DOCTYPE html><html>
<title>PHP Pagination</title>
// Establish Connection to the Database
$con = mysqli_connect(‘localhost’,’root’,”,’mytest’);//Records per page

if (isset($_GET[“page”])) {

$page = $_GET[“page”];


else {



// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;

//Selecting the data from table but with limit
$query = “SELECT * FROM student LIMIT $start_from, $per_page”;
$result = mysqli_query ($con, $query);

<table align=”center” border=”2″ cellpadding=”3″>
while ($row = mysqli_fetch_assoc($result)) {
<tr align=”center”>
<td><?php echo $row[‘name’]; ?></td>
<td><?php echo $row[‘number’]; ?></td>
<td><?php echo $row[‘country’]; ?></td>


//Now select all from table
$query = “select * from student”;
$result = mysqli_query($con, $query);

// Count the total records
$total_records = mysqli_num_rows($result);

//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);

//Going to first page
echo “<center><a href=’pagination.php?page=1′>”.’First Page’.”</a> “;

for ($i=1; $i<=$total_pages; $i++) {

echo “<a href=’pagination.php?page=”.$i.”‘>”.$i.”</a> “;
// Going to last page
echo “<a href=’pagination.php?page=$total_pages’>”.’Last Page’.”</a></center> “;



After saving the file as “pagination.php” if you are using localhost then you can save the file into any folder in your localhost or if you are using online server then upload that file to online server, now simply open the file in your browser and you’ll see a result exactly like the screenshot below:


Video Tutorial for Creating Pagination in PHP

Now if you still have doubts then do watch the video tutorial below, in which I’ve practically explained the code and have taught creating the database in MySQL and also the table for this pagination, so watch the video & learn it practically.

In this pagination tutorial, we’ve used very basic PHP functions & MySQL queries, if you read the source code on the page, you’ll find it easily understandable with documentation. And you can add it to any of your project, just change the database & table names according to your own preferences, and also change the table values to your own ones. But if you are facing difficulties either creating this pagination or adding it to your web pages then don’t hesitate to ask your problem in the comments section below. I’ll be happy to answer & solve your problems.

About Abdul Wali

Abdul Wali is a professional blogger, marketer and web developer working online for last 5 years, he's been deeply involved in PHP, Web Development, SEO and computer video tutorials in different languages i.e English, Urdu and Pashto. You can catch him on and add him on Facebook.

Check Also

web hosting

The Best Web Hosting Providers (Tutorial)

Web hosting is one of the important aspects for establishing a business or website online. If ...

  • Dr.OmegaMale

    Hey how can I style the scrollbar just like you did?

    • Hi, you can use this CSS code to change the scroll bar of any web page, just add to the CSS file of your website or page, you can change the colors accordingly:
      body {
      -webkit-scrollbar { width: 3px; height: 3px;}
      -webkit-scrollbar-button { background-color: #666; }
      -webkit-scrollbar-track { background-color: #999;}
      -webkit-scrollbar-track-piece { background-color: #ffffff;}
      -webkit-scrollbar-thumb { height: 50px; background-color: #666; border-radius: 3px;}
      -webkit-scrollbar-corner { background-color: #999;}}
      -webkit-resizer { background-color: #666;}


      • sir apka ye page suba 268 positions fall kr gya tha or ab 30 pr kese aya ha?

  • Hello,
    Please drop me an email at: and I’ll send you the source code for that project.

  • Haresh Parmar

    Hello Sir

    I Have Creating Pagination. but problem in page is more than so how can implement first12345….50 last

    • You can use this pagination code for any number of pages. did you test it?

      • Haresh Parmar

        I Want to create the same as the Image given below !

        • Mostafa Mohamed

          i want to know that too

  • I have been searching for Mysqli Pagination from past one week I found some but very difficult for me to integrate in my Ajax search project, I found this one its really very nice and easy to understand and install but still my issue is not been resolved, I integrated this with my seach it gives results only on my first page when I click on second page it shows blank and tried with session and giving links but could not able to view results from search. it only works nice to pull out records without search so could any one help me please? that would be really big help for me…

    Thanks & Regards,
    Nicklaus Holt

    • Hi Holt,
      This is very easy pagination to integrate, please let me know via email if I could help you:

    • Barnabas Barnabas


      same problem unable to load other pages they are empty…did u find any solution?

  • haloo brother i am your student and please tell me the time in which you rank this page and i was learn about this page from your video course and your course is really helpful thanks

    • Hi brother, this page took month time for ranking in Google. And you welcome, hope you learned from the course.

  • Mohammad Alauddin

    Thanks for your tremendous video tutorials that helps thousands people like me.. brother, how can create category , subcategory wise product display.. like category>>Bags & Subcategories>>1. Ladies Bags 2. Men Bags etc. Again thanks for your hard work to help others.

    • Hi Mohammad,

      Thanks for stepping by and liking my tutorials.

  • Sukhchain Singh

    Hey Bro Any Tutorial for making categories in Malala CMS?

    • Yes, I’ve created the categories in the advanced CMS which you can find on this site as well as on my channel.

      • Muhammad Qasim

        sir, i have got my mastercard but i’m unable to link this with my paypal account, it shows erros

        • Muhammad Qasim

          plz help

        • What are you doing there?

  • michael

    Great tutorial! is it possible to add a text search functionality?

  • ragupathy

    thanks a lot abdhul wali

  • ragupathy

    how display category and sub category abdhul bai………..pls give video link for that

  • Nii Boye

    works perfect but tells me object not found when i click any page

  • Nii Boye

    when i click any number it gives me empty page but for the first time give me the required page

  • Nii Boye

    First page loads but subsequent pages gives empty record

    • Barnabas Barnabas

      whats up man!
      same problem unable to load other pages they are empty…did u find any solution?

  • dayat

    thank you…you already help me,,

    • you welcome, and glad to know I helped you!

  • satish

    Sir thank u for tutorial .. I requirest please add style to pagination number to looking cool …..

  • satish

    Send me code for adding style for pagination no

    • You can add your own styles.

      • satish

        sir i try to add bootstrap pagination but i face error so please u can send code with bootstrap style pagination

  • Abbas Rasheed Awan

    This script is not working when u have no data in database it will give u this error “mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in ”
    if u replaced $_GET[‘page’] with $_POST[‘page’] this error will be removed then ur pagnation will not work….How Can i Solve this

    • Hi dude, this script is working perfectly.

      • TVG

        Not for me.

  • satish

    my php mysql audio file uploader not work in mobile device why

  • Nikhil

    @onlineustaad:disqus Hai Brother ! I need your help in creating an online voting system . can you help me ?

    • Nikhil


      Need Help on this

  • Lance N

    Very nice tutorial thank you!! Though I wish the video went through to the actual pagination part at the bottom. Seemed it went up to only the limit clause but thank you for making that clear 😉

  • Bryan Cortesiano

    Hello sir! Can i borrow your source code? 😀 I want to add it in my project 🙂

  • joseph samuel

    hellow sir
    could you plz
    convert this code to PDO

  • Md Ashraf

    pls help @onlineustaad:disqus
    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in…

  • Dan-Emanuel Vasii

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:xampphtdocspagination.php on line 40
    at me ,line 40 is :while ($row = mysqli_fetch_array($result)) {
    i do not know why it is showing me this error.Please help me!

  • حیدر ایوب

    Dear Sir Abdul Wali..need some more help…i am pakhtoon and pakistani too so hoping for a quick reply from your end..actually sir i want to know that how can we limit the number of pages..suppose my website have alot of posts means more then 150 and they are dividing into 30 pages and i want to show first page then 1 to 7 and then next and then last page…need technique please

  • Emmanuel

    Thanks for the script, it works perfectly!
    Though, I have a problem to integrate with AJAX.
    Could you please give me some advice for the same?

  • Hardware Tools

    very nice post I liked it sir

  • Valentin George Ututui

    Man you killed it with this post.