php 分页代码。
<?php /** #--------------- #获取序列ID #--------------- */ function getSequence() { $sql = "update sequence set id=last_insert_id(id+1);"; $sql2= "select last_insert_id();"; global $dbuser,$dbpass,$host,$database,$printsql; $link = mysql_connect($host,$dbuser,$dbpass); if(! $link){ return mysql_error(); } mysql_select_db($database); mysql_query("SET NAMES UTF8"); if($printsql) echo "<hr/>".$sql."<hr/>"; mysql_query($sql); if($printsql) echo "<hr/>".$sql2."<hr/>"; $result = mysql_query($sql2); if(mysql_num_rows($result)==0){ mysql_close($link); return ""; } $myrow = mysql_fetch_row($result); $ret=$myrow[0]; mysql_close($link); return $ret; } #--------------- #获取strSql第N条记录中的第N列数据,下标从1开始 #--------------- function getData($strsql,$row,$col) { global $dbuser,$dbpass,$host,$database,$printsql; $link = mysql_connect($host,$dbuser,$dbpass); if(! $link){ return mysql_error(); } mysql_select_db($database); mysql_query("SET NAMES UTF8"); if($printsql) echo "<hr/>".$strsql."<hr/>"; $result = mysql_query($strsql); if(mysql_num_rows($result)==0){ mysql_close($link); return ""; } $i=0; while($myrow = mysql_fetch_row($result)){ if($i==$row-1){ $ret=$myrow[$col-1]; break; } $i=$i+1; } mysql_close($link); return $ret; } #--------------- #获取strSql第N条记录 #--------------- function getRowData($strsql,$row) { global $dbuser,$dbpass,$host,$database,$printsql; $link = mysql_connect($host,$dbuser,$dbpass); if(! $link){ return mysql_error(); } mysql_select_db($database); mysql_query("SET NAMES UTF8"); if($printsql) echo "<hr/>".$strsql."<hr/>"; $result = mysql_query($strsql); if(mysql_num_rows($result)==0){ mysql_close($link); return ""; } $i=0; while($myrow = mysql_fetch_array($result)){ if($i==$row-1){ $ret=$myrow; break; } $i=$i+1; } mysql_close($link); return $ret; } #--------------- #获取strSql记录集存入数组中 #--------------- function getResultSetData($strsql) { global $dbuser,$dbpass,$host,$database,$printsql; $link = mysql_connect($host,$dbuser,$dbpass); if(! $link){ return mysql _error(); } mysql_select_db($database); mysql_query("SET NAMES UTF8"); if($printsql) echo "<hr/>".$strsql."<hr/>"; $result = mysql_query($strsql); if(mysql_num_rows($result)==0){ mysql_close($link); return ""; } while($myrow = mysql_fetch_array($result)){ $ret[]=$myrow; } mysql_close($link); return $ret; } #--------------- #执行strSql #--------------- function executeSql($strsql) { global $dbuser,$dbpass,$host,$database,$printsql; $link = mysql_connect($host,$dbuser,$dbpass); if(! $link){ return mysql_error(); } mysql_select_db($database); mysql_query("SET NAMES UTF8"); if($printsql) echo "<hr/>".$strsql."<hr/>"; mysql_query($strsql); $ret =mysql_affected_rows($link); mysql_close($link); return $ret; } /* 分页 */ function Pager(&$curpage,&$pagesize,&$tsql,&$psql,&$totalpage,&$totalrow,&$pagerset){ $curpage=isset($curpage)?intval($curpage):1;//当前页 $totalpage=0;//总页数 $totalrow=0;//总记录数 if($printsql) echo "<hr/>".$tsql."<hr/>"; if($curpage <= 0){ $curpage=1; } $totalrow=getData($tsql,1,1);//取得总记录数 $totalrow=strlen(totalrow)==0?0:$totalrow; if($totalrow>0){ $totalpage=$totalrow%$pagesize==0?(int)($totalrow/$pagesize):(int)($totalrow/$pagesize)+1; if($curpage>$totalpage){ $curpage=1; } $psql=$psql." limit ".(($curpage-1)*$pagesize).",".$pagesize; if($printsql) echo "<hr/>".$psql."<hr/>"; $pagerset=getResultSetData($psql);//取得当前页记录 } if($totalrow==0||$totalrow=="0"){ $curpage=1;} } ?>