php mysql查询结果分页显示
<?php$mysql_server_name="localhost";$mysql_username='root';$mysql_password='';$mysql_...
<?php
$mysql_server_name="localhost";
$mysql_username='root';
$mysql_password='';
$mysql_database='hp';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
@mysql_connect("localhost","root","") or die("数据库连接失败");
@mysql_select_db("hp") or die("数据库不存在");
mysql_query("set names utf8");
if($_POST['submit']){
$strsql="select * from zichan where 1=1 ";
$strsql .=$_POST['idc'] ? " and idc='$_POST[idc]' ":"";
$strsql .=$_POST['jiguihao'] ? " and jiguihao='$_POST[jiguihao]' ":"";
$strsql .=$_POST['neicun'] ? " and neicun='$_POST[neicun]' ":"";
$strsql .=$_POST['yingpan'] ? " and yingpan='$_POST[yingpan]' ":"";
$strsql .=$_POST['xinghao'] ? " and xinghao='$_POST[xinghao]' ":"";
$strsql .=$_POST['cpu'] ? " and cpu='$_POST[cpu]' ":"";
$strsql .=$_POST['guishu'] ? " and guishu='$_POST[guishu]' ":"";
$strsql .=$_POST['ip'] ? " and ip='$_POST[ip]' ":"";
}
$result=mysql_db_query($mysql_database,$strsql,$conn);
$row=mysql_fetch_row($result);
echo '<font face="verdana">';
echo '<table border="1" cellpadding="1" cellspacing="2">';
// 显示字段名称
echo "</b><tr></b>";
for ($i=0; $i<mysql_num_fields($result); $i++)
{
echo '<td bgcolor="#00FF00"><b>'.
mysql_field_name($result, $i);
echo "</b></td></b>";
}
echo "</tr></b>";
// 定位到第一条记录
mysql_data_seek($result, 0);
// 循环取出记录
while ($row=mysql_fetch_row($result))
{
echo "<tr></b>";
for ($i=0; $i<mysql_num_fields($result); $i++ )
{
echo '<td bgcolor="#00FF00">';
echo $row[$i];
echo '</td>';
}
echo '<td><a href=\'xiugai.php?id='.$row[0].'\'>修改</a>';
echo '</td>';
echo '<td><a href="javascript:void(0)" onclick="if(window.confirm(\'确认删除?\')){window.location.href=\'del.php?id='.$row[0].'\'}">删除</a>';
echo '</td>';
echo "</tr></b>";
}
echo "</table></b>";
echo "</font>";
// 释放资源
mysql_free_result($result);
// 关闭连接
mysql_close($conn);
?>
本页已经可以正常查询出所有结果,就是不能分页,请高人修改下,要求是我复制代码回去什么都不用修改,就能分页显示,谢谢··· 展开
$mysql_server_name="localhost";
$mysql_username='root';
$mysql_password='';
$mysql_database='hp';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
@mysql_connect("localhost","root","") or die("数据库连接失败");
@mysql_select_db("hp") or die("数据库不存在");
mysql_query("set names utf8");
if($_POST['submit']){
$strsql="select * from zichan where 1=1 ";
$strsql .=$_POST['idc'] ? " and idc='$_POST[idc]' ":"";
$strsql .=$_POST['jiguihao'] ? " and jiguihao='$_POST[jiguihao]' ":"";
$strsql .=$_POST['neicun'] ? " and neicun='$_POST[neicun]' ":"";
$strsql .=$_POST['yingpan'] ? " and yingpan='$_POST[yingpan]' ":"";
$strsql .=$_POST['xinghao'] ? " and xinghao='$_POST[xinghao]' ":"";
$strsql .=$_POST['cpu'] ? " and cpu='$_POST[cpu]' ":"";
$strsql .=$_POST['guishu'] ? " and guishu='$_POST[guishu]' ":"";
$strsql .=$_POST['ip'] ? " and ip='$_POST[ip]' ":"";
}
$result=mysql_db_query($mysql_database,$strsql,$conn);
$row=mysql_fetch_row($result);
echo '<font face="verdana">';
echo '<table border="1" cellpadding="1" cellspacing="2">';
// 显示字段名称
echo "</b><tr></b>";
for ($i=0; $i<mysql_num_fields($result); $i++)
{
echo '<td bgcolor="#00FF00"><b>'.
mysql_field_name($result, $i);
echo "</b></td></b>";
}
echo "</tr></b>";
// 定位到第一条记录
mysql_data_seek($result, 0);
// 循环取出记录
while ($row=mysql_fetch_row($result))
{
echo "<tr></b>";
for ($i=0; $i<mysql_num_fields($result); $i++ )
{
echo '<td bgcolor="#00FF00">';
echo $row[$i];
echo '</td>';
}
echo '<td><a href=\'xiugai.php?id='.$row[0].'\'>修改</a>';
echo '</td>';
echo '<td><a href="javascript:void(0)" onclick="if(window.confirm(\'确认删除?\')){window.location.href=\'del.php?id='.$row[0].'\'}">删除</a>';
echo '</td>';
echo "</tr></b>";
}
echo "</table></b>";
echo "</font>";
// 释放资源
mysql_free_result($result);
// 关闭连接
mysql_close($conn);
?>
本页已经可以正常查询出所有结果,就是不能分页,请高人修改下,要求是我复制代码回去什么都不用修改,就能分页显示,谢谢··· 展开
2个回答
展开全部
<?php
$mysql_server_name="localhost";
$mysql_username='root';
$mysql_password='';
$mysql_database='hp';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
@mysql_connect("localhost","root","") or die("数据库连接失败");
@mysql_select_db("hp") or die("数据库不存在");
mysql_query("set names utf8");
$pagesize = $_GET['pagesize'] > 0 ? $_GET['pagesize'] : 10; //默认情况下每页显示10条数据
$pagenum = $_GET['pagenum'] > 0 ? $_GET['pagenum'] : 0;
$query_start = $pagesize * $pagenum;
if($_POST['submit']){
$strsql_add = '';
$strsql_add .=$_POST['idc'] ? " and idc='$_POST[idc]' ":"";
$strsql_add .=$_POST['jiguihao'] ? " and jiguihao='$_POST[jiguihao]' ":"";
$strsql_add .=$_POST['neicun'] ? " and neicun='$_POST[neicun]' ":"";
$strsql_add .=$_POST['yingpan'] ? " and yingpan='$_POST[yingpan]' ":"";
$strsql_add .=$_POST['xinghao'] ? " and xinghao='$_POST[xinghao]' ":"";
$strsql_add .=$_POST['cpu'] ? " and cpu='$_POST[cpu]' ":"";
$strsql_add 裤宴.=$_POST['guishu'] ? " and guishu='$_POST[guishu]' ":"";
$strsql_add .=$_POST['ip'] ? " and ip='$_POST[ip]' ":"";
}
$strsql="select * from zichan where 1=1 $strsql_add limit $query_start, $pagesize";
$result=mysql_db_query($mysql_database,$strsql,$conn);
$row=mysql_fetch_row($result);
$nextpage_num = $pagenum++;
$lastpage_num = ceil(count($row)/$pagesize);
echo '<font face="verdana">';
echo '<table border="1" cellpadding="1" cellspacing="2">';
早磨// 显示字段名称
echo "</b><tr></b>";
陆纯斗for ($i=0; $i<mysql_num_fields($result); $i++)
{
echo '<td bgcolor="#00FF00"><b>'.
mysql_field_name($result, $i);
echo "</b></td></b>";
}
echo "</tr></b>";
// 定位到第一条记录
mysql_data_seek($result, 0);
// 循环取出记录
while ($row=mysql_fetch_row($result))
{
echo "<tr></b>";
for ($i=0; $i<mysql_num_fields($result); $i++ )
{
echo '<td bgcolor="#00FF00">';
echo $row[$i];
echo '</td>';
}
echo '<td><a href=\'xiugai.php?id='.$row[0].'\'>修改</a>';
echo '</td>';
echo '<td><a href="javascript:void(0)" onclick="if(window.confirm(\'确认删除?\')){window.location.href=\'del.php?id='.$row[0].'\'}">删除</a>';
echo '</td>';
echo "</tr></b>";
}
echo "</table></b>";
echo "</font>";
// 释放资源
mysql_free_result($result);
// 关闭连接
mysql_close($conn);
?>
html页面要增加的参数:
首页 增加 &pagenum=0 或者 不加
下一页 &pagenum= $nextpage_num
尾页 &pagenum= $lastpage_num
追问
大神能帮我把html的语句也写一下吗,这个上一页要怎么添加
追答
上一页 需要在php代码里的 $nextpage_num = $pagenum++; 这一句后增加
$prevpage_num = $pagenum - 1 < 0 ? 0 : $pagenum - 1;
html 参数 &pagenum=$prevpage_num
html语句的话,要根据你的整个代码情况来看,你给出的这个文件内容太少。无法直接加。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |