PHP efficient access to data paging
Last Update:2016-06-23
Source: Internet
Author: User
Mysql.php get the records in the database, complete personal experience Summary, for reference only!
/**
*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;
}
/*
Page out
*/
Function Pager (& $curpage,& $pagesize,& $tsql,& $psql,& $totalpage,& $totalrow,& $pagerset ){
$curpage =isset ($curpage)? Intval ($curpage): 1;//Current Page
$totalpage =0;//Total Pages
Total number of records $totalrow =0;//
if ($printsql) echo " ". $tsql." ";
if ($curpage <= 0) {
$curpage = 1;
}
$totalrow =getdata ($tsql, 1, 1);//Get Total record Count
$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;}
}
?>