Php efficient data retrieval page

Source: Internet
Author: User
Tags psql
Php efficiently retrieves data by page. mysql. php retrieves records from the database. this is a summary of my personal experience and is for your reference only!
/**
* Basic functions of PHP + MYSQL databases
* Http://blog.csdn.net/yown
*/
######################################## ####
# Obtain the sequence 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;
}
######################################## ####
# Obtain the data in column N of the Nth record of strSql. the subscript starts 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;

}

######################################## ####
# Obtain the Nth record of strSql
######################################## ####
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;

}

######################################## ####
# Getting strSql record set and saving it to the array
######################################## ####
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;
}

######################################## ####
# Execute 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
$ Totalpage = 0; // total number of pages
$ Totalrow = 0; // total number of records
If ($ printsql) echo" ". $ Tsql ." ";
If ($ curpage <= 0 ){
$ Curpage = 1;
}

$ Totalrow = getData ($ tsql,); // get the total number of records
$ 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 the 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.