PHP MySQL Pagination Next Previous Example

Mar 16, 2022 . Admin

Hello dev,

I am going to explain you how to create PHP MySQL Pagination Next Previous Example. You will learn Pagination Code In PHP MySQL With Next And Previous. In side this article we will see How to create Pagination with PHP.

This article will give you simple example of Next and Previous buttons in PHP with MYSQL database. We will use get simple How to Create Pagination in PHP and MYSQL.

I will give you simple Example of How to create Pagination with PHP and MySql.

So, let's see bellow solution:

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "aatman";

    $conn = new mysqli($servername,$username,$password,$dbname);

        die ('connection faild:'.$conn->connect_error);
    include "connection.php";

    $rowperpage = 5;
    $row = 0;

        // Previous Button
        $row = $_POST['row'];
        $row -= $rowperpage;
        if( $row < 0 ){
            $row = 0;

        // Next Button
        $row = $_POST['row'];
        $allcount = $_POST['allcount'];

        $val = $row + $rowperpage;
        if( $val < $allcount ){
            $row = $val;

<!doctype html>
    <title>PHP MySQL Pagination Next Previous Example</title>
    <link href="" rel="stylesheet">
<body class="bg-dark">
    <div class="container mt-5">
        <div class="card">
            <div class="card-header">
                <h3 class="text-center">PHP MySQL Pagination Next Previous Example -</h3>
            <div class="card-body">
                <table class="table table-striped text-center table-bordered">

                            $sql = "SELECT COUNT(*) AS cntrows FROM customers";
                            $result = mysqli_query($conn,$sql);
                            $fetchresult = mysqli_fetch_array($result);
                            $allcount = $fetchresult['cntrows'];
                            $sql = "SELECT * FROM customers  ORDER BY ID ASC limit $row,".$rowperpage;
                            $result = mysqli_query($conn,$sql);
                            $sno = $row + 1;
                            while($fetch = mysqli_fetch_array($result)){
                                $name = $fetch['name'];
                                $salary = $fetch['city'];
                                $age = $fetch['age'];
                        <td><?php echo $sno; ?></td>
                        <td><?php echo $name; ?></td>
                        <td><?php echo $age; ?></td>
                        <td><?php echo $salary; ?></td>
                            $sno ++;
                <form method="post" action="">
                    <div class="d-flex justify-content-center">
                        <input type="hidden" name="row" value="<?php echo $row; ?>">
                        <input type="hidden" name="allcount" value="<?php echo $allcount; ?>">
                        <input type="submit" class="btn btn-primary mx-2" name="but_prev" value="<< Previous">
                        <input type="submit" class="btn btn-primary mx-2" name="but_next" value="Next >>">


It will help you...