Efficient access to data paging in PHP
Last Update:2017-02-28
Source: Internet
Author: User
Mysql.php get the records in the database, complete personal experience Summary, only for reference!
-->/**
*php+mysql Database Basic functions
*http://blog.csdn.net/yown
*/
############################################
#获取序列ID
############################################
function Getsequence () {
$sql = "Update sequence set id=last_insert_id (id+1);";
$sql 2= "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 " ". $sql 2." ";
$result = mysql_query ($sql 2);
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列数据, subscript starting from 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;
}
/*
Paging
*/
Function Pager (& $curpage,& $pagesize,& $tsql,& $psql,& $totalpage,& $totalrow,& $pagerset ){
$curpage =isset ($curpage) intval ($curpage): 1;//Current Page
Total Pages $totalpage =0;//
Total Records of $totalrow =0;//
if ($printsql) echo " ". $tsql." ";
if ($curpage <= 0) {
$curpage = 1;
}
$totalrow =getdata ($tsql, 1, 1);//Total number of records obtained
$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);//Get current page record
}
if ($totalrow ==0$totalrow== "0") {$curpage = 1;}
}
?>