Home > php教程 > php手册 > php 结果集的分页实现代码

php 结果集的分页实现代码

WBOY
Release: 2016-06-13 12:24:36
Original
1697 people have browsed it

复制代码 代码如下:


@mysql_connect("localhost", "root","1981427") //连接数据库服务器
or die("数据库服务器连接失败");
@mysql_select_db("test") //选择数据库mydb
or die("数据库不存在或不可用");
$query = @mysql_query("select * from tablename1") //执行用于计算页数的SQL语句
or die("SQL语句执行失败");
$pagesize = 5; //设置每页记录数
$sum = mysql_numrows($query); //计算总记录数
if($sum % $pagesize == 0) //计算总页数
$total = (int)($sum/$pagesize);
else
$total = (int)($sum/$pagesize) + 1;
if (isset($_GET['page'])) //获得页码
{
$p = (int)$_GET['page'];
}
else
{
$p = 1;
}
$start = $pagesize * ($p - 1); //计算起始记录
//执行查询当前页记录的SQL语句
$query = @mysql_query("select * from tablename1 limit $start, $pagesize")
or die("SQL语句执行失败");
echo "

"; //输出表头
//通过循环的方式输出从第0行到最大的一行的所有记录
while($row = mysql_fetch_array($query))
{
$serial_no = $row['id']; //输出第$i行的serial_no列
$name = $row['username']; //输出第$i行的name列
$salary = $row['password']; //输出第$i行的salary列
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "
$serial_no$name$salary
"; //输出表尾
if($p > 1) //当前页不是第一页时,输出上一页的链接
{
$prev = $p - 1;
echo "上一页 ";
}
if($p {
$next = $p + 1;
echo "下一页";
}
?>
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template