Paging can be made according to the size of the record in the database of the complex, especially when you want to link the group number to display the next / previous links. In this tutorial, we will focus on creating a simple PHP MYSQL tab.
Let’s create a MySQL database

A SQL file is included in the attached full script file. First create a MySQL database, and then import the .sql file. (This is just the purpose of this tutorial, but you can use your own database, and if you have created).

Database Connectivity:

Let’s start with the database connection files stored MySQL database user name and password, and also uses PHP mysqli_connect connect to MySQL (). If you find that we are using PHP mysqli_connect () here, is not an ordinary mysql_connect () function, I = on behalf of improved promotion, development to take advantage of the new system, new features in MySQL. PHP encourage everyone to use mysqli * instead of the regular mysqli * expansion, which will be completely removed in a future version of PHP.

Create connection.php file, and these PHP code (provided below) on the inside.

$db_username = 'root'; // Your MYSQL Username.
$db_password = ''; // Your MYSQL Password.
$db_name = 'database_name_here'; // Your Database name.
$db_host = 'localhost';
  
$conDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('Error: Could not connect to database.');

Paging function:

Let us create a paging function, and store it in your functions.php file.
 (Functions.php file we use to store this, we prefer 
this method, because this way, we can easily 
add more useful features, we file).
function pagination($query,$per_page=10,$page=1,$url='?'){   
    global $conDB; 
    $query = "SELECT COUNT(*) as `num` FROM {$query}";
    $row = mysqli_fetch_array(mysqli_query($conDB,$query));
    $total = $row['num'];
    $adjacents = "2"; 
      
    $prevlabel = "‹ Prev";
    $nextlabel = "Next ›";
    $lastlabel = "Last ››";
      
    $page = ($page == 0 ? 1 : $page);  
    $start = ($page - 1) * $per_page;                               
      
    $prev = $page - 1;                          
    $next = $page + 1;
      
    $lastpage = ceil($total/$per_page);
      
    $lpm1 = $lastpage - 1; // //last page minus 1
      
    $pagination = "";
    if($lastpage > 1){   
        $pagination .= "
<ul class='pagination'>";
        $pagination .= "
<li class='page_info'>Page {$page} of {$lastpage}</li>

";
              
            if ($page > 1) $pagination.= "
<li><a href='{$url}page={$prev}'>{$prevlabel}</a></li>

";
              
        if ($lastpage < 7 + ($adjacents * 2)){   
            for ($counter = 1; $counter <= $lastpage; $counter++){
                if ($counter == $page)
                    $pagination.= "
<li><a class='current'>{$counter}</a></li>

";
                else
                    $pagination.= "
<li><a href='{$url}page={$counter}'>{$counter}</a></li>

";                    
            }
          
        } elseif($lastpage > 5 + ($adjacents * 2)){
              
            if($page < 1 + ($adjacents * 2)) {
                  
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
                    if ($counter == $page)
                        $pagination.= "
<li><a class='current'>{$counter}</a></li>

";
                    else
                        $pagination.= "
<li><a href='{$url}page={$counter}'>{$counter}</a></li>

";                    
                }
                $pagination.= "
<li class='dot'>...</li>

";
                $pagination.= "
<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>

";
                $pagination.= "
<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>

";  
                      
            } elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
                  
                $pagination.= "
<li><a href='{$url}page=1'>1</a></li>

";
                $pagination.= "
<li><a href='{$url}page=2'>2</a></li>

";
                $pagination.= "
<li class='dot'>...</li>

";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
                    if ($counter == $page)
                        $pagination.= "
<li><a class='current'>{$counter}</a></li>

";
                    else
                        $pagination.= "
<li><a href='{$url}page={$counter}'>{$counter}</a></li>

";                    
                }
                $pagination.= "
<li class='dot'>..</li>

";
                $pagination.= "
<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>

";
                $pagination.= "
<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>

";      
                  
            } else {
                  
                $pagination.= "
<li><a href='{$url}page=1'>1</a></li>

";
                $pagination.= "
<li><a href='{$url}page=2'>2</a></li>

";
                $pagination.= "
<li class='dot'>..</li>

";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
                    if ($counter == $page)
                        $pagination.= "
<li><a class='current'>{$counter}</a></li>

";
                    else
                        $pagination.= "
<li><a href='{$url}page={$counter}'>{$counter}</a></li>

";                    
                }
            }
        }
          
            if ($page < $counter - 1) {
                $pagination.= "
<li><a href='{$url}page={$next}'>{$nextlabel}</a></li>

";
                $pagination.= "
<li><a href='{$url}page=$lastpage'>{$lastlabel}</a></li>

";
            }
          
        $pagination.= "</ul>

";        
    }
      
    return $pagination;
}

Displaying database records and pagination:

include_once('connection.php');
include_once('functions.php');
 
$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
if ($page &amp;amp;amp;amp;lt;= 0) $page = 1;
 
$per_page = 10; // Set how many records do you want to display per page.
 
$startpoint = ($page * $per_page) - $per_page;
 
$statement = "`records` ORDER BY `id` ASC"; // Change `records` according to your table name.
  
$results = mysqli_query($conDB,"SELECT * FROM {$statement} LIMIT {$startpoint} , {$per_page}");
 
if (mysqli_num_rows($results) != 0) {
     
    // displaying records.
    while ($row = mysqli_fetch_array($results)) {
        echo $row&amp;amp;amp;amp;amp;#91;'name'&amp;amp;amp;amp;amp;#93; . '
';
    }
  
} else {
     echo "No records are found.";
}
 
 // displaying paginaiton.
echo pagination($statement,$per_page,$page,$url='?');

Simple CSS:

ul.pagination {
    text-align:center;
    color:#829994;
}
ul.pagination li {
    display:inline;
    padding:0 3px;
}
ul.pagination a {
    color:#0d7963;
    display:inline-block;
    padding:5px 10px;
    border:1px solid #cde0dc;
    text-decoration:none;
}
ul.pagination a:hover, 
ul.pagination a.current {
    background:#0d7963;
    color:#fff; 
}

Complete script tab

We have just created a simple PHP MYSQL paging function. You can download the complete script from the right-hand column. Click the button to download the attachment.