利用sql的limit属性对mysql数据库
2007-11-11 16:14:51 来源:WEB开发网//利用sql的limit属性对MySQL(和PHP搭配之最佳组合)数据库
<?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(和PHP搭配之最佳组合)_connect("localhost","http","something");
$select=MySQL(和PHP搭配之最佳组合)_select_db('book',$connect);
$query=MySQL(和PHP搭配之最佳组合)_query('select count(*) as total from main',$connect);
$totalrecord=MySQL(和PHP搭配之最佳组合)_result($query,0,"total");
$query=MySQL(和PHP搭配之最佳组合)_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(和PHP搭配之最佳组合)_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(和PHP搭配之最佳组合)_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自由联盟
更多精彩
赞助商链接