PHP efficient access to data paging

Source: Internet
Author: User
Tags psql
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;}

}

?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.