1、连接MYSQL数据库代码
<?php
$connec=mysql_connect("localhost","root","root") or die("不能连接数据库服务器: ".mysql_error());
mysql_select_db("liuyanben",$connec) or die ("不能选择数据库: ".mysql_error());
mysql_query("set names 'gbk'");
?>
2、读取数据库,并实现循环输出
<?php
$sql="select * from liuyan order by ly_id desc";
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
<table width="476" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="135" height="30"><div align="center">留言主题</div></td>
<td width="204" height="30"><div align="center">留言时间</div></td>
<td width="129" height="30"><div align="center">留言人</div></td>
</tr>
<tr bgcolor="#CC9999">
<td height="30"><?= $rs["ly_title"]?>
<div align="center"></div></td>
<td height="30"><?= $rs["ly_time"]?>
<div align="center"></div></td>
<td height="30"><?= $rs["ly_author"]?>
<div align="center"></div></td>
</tr>
<tr>
<td height="30"><div align="center"><a href="huifu.php?id=<?php echo $rs["ly_id"]?>">回复留言</a></div></td>
<td height="30"><div align="center"><a href="viewhuifu.php?id=<?php echo $rs["ly_id"]?>">查看回复信息</a></div></td>
<td height="30"><div align="center">已有回复</div></td>
</tr>
</table>
<?php
}
?>
3、如何实现分页,包括两个函数,两个调用
1)两个函数
<?
//分页函数
function genpage(&$sql,$page_size=2)
{
global $prepage,$nextpage,$pages,$sums; //out param
$page = $_GET["page"];
$eachpage = $page_size;
$pagesql = strstr($sql," from ");
$pagesql = "select count(*) as ids ".$pagesql;
$conn = mysql_query($pagesql) or die(mysql_error());
if($rs = mysql_fetch_array($conn)) $sums = $rs[0];
$pages = ceil(($sums-0.5)/$eachpage)-1;
$pages = $pages>=0?$pages:0;
$prepage = ($page>0)?$page-1:0;
$nextpage = ($page<$pages)?$page+1:$pages;
$startpos = $page*$eachpage;
$sql .=" limit $startpos,$eachpage ";
}
//显示分页
function showpage()
{
global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
$shownum =10/2;
$startpage = ($page>=$shownum)?$page-$shownum:0;
$endpage = ($page+$shownum<=$pages)?$page+$shownum:$pages;
echo "共".($pages+1)."页: ";
if($page>0)echo "<a href=$PHP_SELF?page=0$queryString>首页</a>";
if($startpage>0)
echo " ... <b><a href=$PHP_SELF?page=".($page-$shownum*2)."$queryString>«</a></b>";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " <b>[".($i+1)."]</b> ";
else echo " <a href=$PHP_SELF?page=$i$queryString>".($i+1)."</a> ";
}
if($endpage<$pages)
echo "<b><a href=$PHP_SELF?page=".($page+$shownum*2)."$queryString>»</a></b> ... ";
if($page<$pages)
echo "<a href=$PHP_SELF?page=$pages$queryString>尾页</a>";
}
//显示带分类的分页
function showpage1()
{
$fenlei=$_GET["fenleiid"];
global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
$shownum =10/2;
$startpage = ($page>=$shownum)?$page-$shownum:0;
$endpage = ($page+$shownum<=$pages)?$page+$shownum:$pages;
echo "共".($pages+1)."页: ";
if($page>0)echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=0$queryString>首页</a>";
if($startpage>0)
echo " ... <b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page-$shownum*2)."$queryString>«</a></b>";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " <b>[".($i+1)."]</b> ";
else echo " <a href=$PHP_SELF?fenleiid=$fenlei&page=$i$queryString>".($i+1)."</a> ";
}
if($endpage<$pages)
echo "<b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page+$shownum*2)."$queryString>»</a></b> ... ";
if($page<$pages)
echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=$pages$queryString>尾页</a>";
}
?>
2)两个调用
第一个
<?php
$sql="select * from liuyan order by ly_id desc";
genpage($sql); //只需要正常代码加上这一行就ok。
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
第二个
<?php
}
?>
<?php
showpage(); //显示页
?>
<?php
mysql_close();
?>
4、服务器端包含
|