Home > Backend Development > PHP Tutorial > php高效率获取数据分页

php高效率获取数据分页

WBOY
Release: 2016-06-13 12:00:47
Original
1272 people have browsed it

php高效获取数据分页
mysql.php 获取数据库中的记录,完全个人经验总结,仅供参考!

/**
*PHP+MYSQL数据库基本功能
*http://blog.csdn.net/yown
*/
############################################
#获取序列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 "


".$sql."
";
mysql_query($sql);
if($printsql) echo "
".$sql2."
";

$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 "
".$strsql."
";

$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 "
".$strsql."
";

$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 "
".$strsql."
";

$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 "
".$strsql."
";

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 "
".$tsql."
";

 if($curpage
   $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 "
".$psql."
"; 

    $pagerset=getResultSetData($psql);//取得当前页记录
 }
 if($totalrow==0||$totalrow=="0"){ $curpage=1;}
 
}

?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template