|
技术资料 > PHP技术 > 数据库操作 : 利用sql的limit属性对mysql数据库 |
利用sql的limit属性对mysql数据库 March 25,2004 |
//利用sql的limit属性对mysql数据库
<?php
function pagetorecord($page,$pagecounts){
$result=($page-1)*$pagecounts;
return $result;
}
function pagech($page,$j){
if($j==1){
$p=$page+1;}
else{
$p=$page-1;}
return $p;
}
$pagecounts=20;
if($startpage){
$startpage=$startpage;
}
else{
$startpage=1;
}
$sqltr="select * from main order by sm limit ".pagetorecord($startpage,$pagecounts).",".$pagecounts;
$connect=mysql_connect("localhost","http","something");
$select=mysql_select_db('book',$connect);
$query=mysql_query('select count(*) as total from main',$connect);
$totalrecord=mysql_result($query,0,"total");
$query=mysql_query($sqltr,$connect);
//-------------------------------
echo "<table border=1 cellpadding=0 cellspacing=0>";
echo"<tr><td>序号</td><td>标准书号</td><td>书名</td><td>出版社</td><td>定价</td></tr>";
$i=1;
while($array=mysql_fetch_array($query)){
printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%f</td></tr>",
$i,$array[bzsh],$array[sm],$array[cbs],$array[dj]);
$i++;
}
echo"</table>";
echo"<hr>";
mysql_close($connect);
if(0==$totalrecord%$pagecounts){
$totalpage=floor($totalrecord/$pagecounts);}
else{
$totalpage=floor($totalrecord/$pagecounts)+1;
}
//echo $sqltr;
if($startpage==1){
if($totalpage>1){
echo"首页 ";
echo"前页 ";
echo "<a href=".basename($PHP_SELF)."?startpage=".pagech($startpage,1).">下页 </a>";
echo "<a href=".basename($PHP_SELF)."?startpage=".$totalpage.">尾页</a> 共".$totalpage."页 第".$startpage."页";
}
else{
echo"首页 ";
echo"前页 ";
echo"下页 ";
echo"尾页 ";
}
}
elseif($startpage==$totalpage){
echo "<a href=".basename($PHP_SELF)."?startpage=1>首页 </a>";
echo "<a href=".basename($PHP_SELF)."?startpage=".pagech($startpage,-1).">前页 </a>";
echo"下页 ";
echo"尾页 共".$totalpage."页 第".$startpage."页";
}
else{
echo "<a href=".basename($PHP_SELF)."?startpage=1>首页 </a>";
echo "<a href=".basename($PHP_SELF)."?startpage=".pagech($startpage,-1).">前页 </a>";
echo "<a href=".basename($PHP_SELF)."?startpage=".pagech($startpage,1).">下页 </a>";
echo "<a href=".basename($PHP_SELF)."?startpage=".$totalpage.">尾页</a> 共".$totalpage."页 第".$startpage."页";
}
?>
原作者:不详
来源:中国PHP自由联盟
|
|
Copyright © 2001-2008 Shenzhen Hiblue Software Team All rights reserved