In this PHP tutorial, we will see how to perform pagination in PHP . When you use SQL SELECT statement to fetch records from database, it may result in thousands of records. To display these all records in a single page is not good idea at all, for that we will divide our all records into pages.

With Paging, we will display records in multiple pages instead of single page. For that we will use LIMIT clause in SELECT statement.

Lets see example of fetching records with PHP Pagination :

Here i have used a simple table having 20 records, and 3 columns no, name and city. We will  display only 7 records per page.

// Logic For Paging

 $rec_page=7; // Number of records to be displayed on a page
 if (isset($_GET["page"]))
 {
        $page = $_GET["page"]; 
 } 
 else
 { 
         $page=1; 
 }; 
 
       $start = ($page-1) * $rec_page; 
       $res =mysql_query("SELECT * FROM temp LIMIT $start, $rec_page"); 

//Logic for displaying Paging

Paging PHP

         $res = mysql_query("select * from temp"); //run the query       
        $total_recs = mysql_num_rows($res); //count number of records
        $total_pages = ceil($total_recs / $rec_page); 

        echo "<a href='paging_php.php?page=1'>". '|< '. "</a> "; // For 1st page 

         for ($i=1; $i<=$total_pages; $i++) 
         { 
              echo "<a href='paging_php.php?page=".$i."'>". $i. "</a> "; 
         }; 
        echo "<a href='paging_php.php?page=$total_pages'>". '>| '."</a> "; // For last page

<a href="https://freefeast.info/wp-content/uploads//2014/11/PHP-Pagination.jpg"><img class="aligncenter size-full wp-image-137060" src="https://freefeast.info/wp-content/uploads//2014/11/PHP-Pagination.jpg" alt="PHP Pagination" width="716" height="429" /></a>

// Complete Code for PHP Pagination 

<html>
  <body>
  <center>
         <h2 align="center">Employee Data</h2>
         <table border="2">
           <tr align="center">
               <th>No </th> <th>Name</th> <th>City</th> 
           </tr>
        
         <?php
                $con=mysql_connect("localhost","root","");   // Connect to MySQL
                $res=mysql_select_db("employee",$con);       // Connect to database

               $rec_page=7; // Number of records to be displayed on a page
 
               if (isset($_GET["page"]))
               {
                    $page = $_GET["page"]; 
               } 
               else
               { 
                       $page=1;  
               }; 
 
               $start = ($page-1) * $rec_page; 
               $res =mysql_query("SELECT * FROM temp LIMIT $start, $rec_page");   // fetching only 7 records
 
               while($r=mysql_fetch_row($res)) 
              {
                  echo "<tr>";
                  echo "<td align='center'width='100'>$r[0]</td>"; 
                  echo "<td align='center' width='150'> $r[1]</td>";
                  echo "<td align='center' width='150'> $r[2]</td>";
                  echo "</tr>";
              }
 ?>
        </table>
<?php
        $res = mysql_query("select * from temp"); //run the query
        $total_recs = mysql_num_rows($res); //count number of records
        $total_pages = ceil($total_recs / $rec_page); 

        echo "<a href='paging_php.php?page=1'>". '|< '. "</a> "; // For 1st page 

         for ($i=1; $i<=$total_pages; $i++) 
         { 
              echo "<a href='paging_php.php?page=".$i."'>". $i. "</a> "; 
         }; 
        echo "<a href='paging_php.php?page=$total_pages'>". '>| '."</a> "; // For last page
?>
 </center>
 </body>
 </html>