一个简单的mysql数据库分页的程序模板_MySQL
程序员文章站
2022-06-01 17:13:11
...
下面是一个简单的php连接mysql数据库进行数据分页显示的模版.可以按注释说明信息进行修改,里面的sql语句是可以自己改的.
注意分析和观察里面相关分页部分的代码的书写和实现的方式.
$link = mysql_connect('localhost', 'root', '') or die('mysql database connect error');
mysql_select_db('your database') or die('the selected database is not exist');
?>
//这里插入你的html代码,
$sql = 'select count(*) count from your_table';
$result = mysql_query($sql) or die(mysql_errno().": ".mysql_error()."\n");
$rs=mysql_fetch_object($result);
$recountCount = $rs->count;
$show = 20;
$totalPage = ceil($recountCount/$show);
$page = (isset($_GET['page']) && $_GET['page']>=0)? $_GET['page']: 0;
$isLast = ($page==($totalPage-1))? true: false;
$hasNoPre = ($page==0)? true: false;
$hasNoNext = ($page==$totalPage-1)? true: false;
$isFirst = ($page==0)? true:false;
$start = $page*$show;
mysql_free_result($result);
?>
//这里插入你的html代码,
$sql = "select * from your_table limit $start,$show";
$result = mysql_query($sql) or die(mysql_errno().": ".mysql_error()."\n");
while($rs=mysql_fetch_object($result)){
//这个循环里的html代码自己更具实际情况修改
echo $rs->art_id;
echo "
";
}
mysql_free_result($result);
?>
$str = "共 $recountCount 条记录,当前第 ".($page+1)."/$totalPage 页 ";
$str .= $isFirst? "首页 " : "首页 ";
$str .= $hasNoPre? "上一页 " : "上一页 ";
$str .= $hasNoNext? "下一页 " : "下一页 ";
$str .= $isLast? "尾页 " : "尾页";
echo $str;
?>
注意分析和观察里面相关分页部分的代码的书写和实现的方式.
$link = mysql_connect('localhost', 'root', '') or die('mysql database connect error');
mysql_select_db('your database') or die('the selected database is not exist');
?>
//这里插入你的html代码,
$sql = 'select count(*) count from your_table';
$result = mysql_query($sql) or die(mysql_errno().": ".mysql_error()."\n");
$rs=mysql_fetch_object($result);
$recountCount = $rs->count;
$show = 20;
$totalPage = ceil($recountCount/$show);
$page = (isset($_GET['page']) && $_GET['page']>=0)? $_GET['page']: 0;
$isLast = ($page==($totalPage-1))? true: false;
$hasNoPre = ($page==0)? true: false;
$hasNoNext = ($page==$totalPage-1)? true: false;
$isFirst = ($page==0)? true:false;
$start = $page*$show;
mysql_free_result($result);
?>
//这里插入你的html代码,
$sql = "select * from your_table limit $start,$show";
$result = mysql_query($sql) or die(mysql_errno().": ".mysql_error()."\n");
while($rs=mysql_fetch_object($result)){
//这个循环里的html代码自己更具实际情况修改
echo $rs->art_id;
echo "
";
}
mysql_free_result($result);
?>
$str = "共 $recountCount 条记录,当前第 ".($page+1)."/$totalPage 页 ";
$str .= $isFirst? "首页 " : "首页 ";
$str .= $hasNoPre? "上一页 " : "上一页 ";
$str .= $hasNoNext? "下一页 " : "下一页 ";
$str .= $isLast? "尾页 " : "尾页";
echo $str;
?>