Code Example of php Native Database Paging

  • 2021-11-29 06:21:40
  • OfStack

The code for php native database paging is as follows:


<?php
header("Content-type:text/html;charset=utf-8");
//  Connect to a database 
$con = mysql_connect(" Database address "," Database account number "," Database password ");
if (!$con){die('Could not connect: ' . mysql_error());}
 
mysql_select_db(" Database name ", $con);
 
//  Number of items displayed per page 
$pageLine = 5;
 
//  Calculate the total number of records 
$ZongPage = mysql_query("select count(*) from  Table name ");
 
//  Calculate the total number of pages 
$sum = mysql_fetch_row($ZongPage);
$pageCount = ceil($sum[0]/$pageLine);  
 
//  Define page number variables 
@$tmp = $_GET['page'];
 
 
//  Calculate the starting value of paging 
$num = ($tmp - 1) * $pageLine;
 
//  Query statement 
$result = mysql_query("SELECT  Field  FROM  Table name  ORDER BY id DESC LIMIT " . $num . ",$pageLine");
 
//  Traversal output 
while($row = mysql_fetch_array($result))
 {
   echo $row[' Field '];
   echo "<br/>";
 }
 
// Paging button 
// Upper 1 Page 
$lastpage = $tmp-1;
// Under 1 Page 
$nextpage = $tmp+1;
 
// Prevent overturning 
if (@$tmp > $pageCount) {
  echo " There are not so many pages, please go back ";
}
 
// If the page number is greater than the total number of pages, the display is gone 
if(@$tmp <= 1){
  echo "<a href=\"fenye.php?page=$nextpage\"> Under 1 Page </a>";
}else if(@$tmp > 1 && @$tmp < $pageCount){
  echo "<a href=\"fenye.php?page=$lastpage\"> Upper 1 Page </a>";
  echo "<a href=\"fenye.php?page=$nextpage\"> Under 1 Page </a>";
}else if(@$tmp = $pageCount){
  echo "<a href=\"fenye.php?page=$lastpage\"> Upper 1 Page </a>";
}
 
//  Close the database connection 
mysql_close($con);
?>

mysql this connection library should have been used by very few people, but you can learn its writing and principles, thank you for your study and support for this site.


Related articles: