Php + mysql paging

Source: Internet
Author: User
Php + mysql paging: php + mysql paging, post code first, and then describe the problem
Code:

$ Page = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1; // obtain the value of the page in page = 18. if there is no page, the page number is 1. $ Searchinfo = isset ($ _ GET ['search'])? ($ _ GET ['search']): "-1"; $ search = ($ searchinfo = 'Enter the keyword ')? -1: $ searchinfo; $ num = 1; // three pieces of data are displayed on each page./* First, we need to obtain the actual amount of data in the database to determine the specific number of pages to be divided, the specific formula is that the total database is divided by the number of entries displayed on each page, more than one. That is to say, 10/3 = 3.3333 = 4 there is a remainder, and we need to enter. */If ($ config [NEEDDB]) {// if need db $ dbLink = DBPool: getLink ($ config [DBDRIVER]); $ daoImpl = DAOImpl :: getImpl ($ dbLink, $ config [TABLEPRE] [BACKEND]); $ config [DBLINK] = $ dbLink; $ config [DAOIMPL] = $ daoImpl ;} $ adRs = $ config [DAOIMPL]-> adSearchCount ($ search); // $ adList = rs2Array ($ adRs); $ adList = mysql_fetch_array ($ adRs ); mysql_free_result ($ adRs); // DBPool: closeLink ($ config [DBLINK]); if (count ($ adList)> 0) {$ tota L = $ adList [0] ['cn']; // query all data} else {$ total = 0 ;}$ url = 'Views/adSearchResult. php '; // Obtain the URL of the current page // calculate the page number $ pagenum = ceil ($ total/$ num); // obtain the total page number, also the last page $ page = min ($ pagenum, $ page); // Get the homepage $ prepg = $ page-1; // Previous page $ nextpg = ($ page = $ pagenum? 0: $ page + 1); // Next page $ offset = ($ page-1) * $ num; // obtain the value of the first parameter of limit, if the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10. // Start pagination navigation bar code: $ pagenav = "display". ($ Total? ($ Offset + 1): 0 )."-". Min ($ offset + 10, $ total )."A total of $ total records "; // if there is only one page, the function will jump out: // if ($ pagenum <= 1) return false; $ pagenav. = "homepage"; if ($ prepg) $ pagenav. = "previous page"; else $ pagenav. = "previous page"; if ($ nextpg) $ pagenav. = ""; else $ pagenav. = "back page"; $ pagenav. = "Last page"; $ pagenav. ="Page, total $ pagenum page "; // If the incoming page number parameter is greater than the total page number, the Error message If ($ page> $ pagenum) {Echo" Error: can Not Found The page ". $ page; Exit;} $ adSearchRs = $ config [DAOIMPL]-> getADSearchInfo ($ search, $ offset, $ page); $ adSearchList = rs2Array ($ adSearchRs ); // mysql_free_result ($ adSearchRs); foreach ($ adSearchList as $ it) {// While ($ it = mysql_fetch_array ($ adSearchRs) {Echo'

'. $ It ['title'].'

'. $ It ['subobject'].'

'. $ It ['Pic _ url'].'

'. $ It ['advert _ url'].'

'. $ It ['If _ valid'].'

';} // Display data echo $ pagenav; // output paging navigation



Problem Description: Paging logic: calculate the number of records under the query condition, and then calculate the paging data based on the page number and the number of entries per page (two pages exist ).
The first query is OK, and the number of data entries meeting the condition is returned ($ adRs = $ config [DAOIMPL]-> adSearchCount ($ search );); however, an error is reported after the second query is executed. the error message is:
The corresponding code is While ($ it = mysql_fetch_array ($ adSearchRs. In addition, data is returned when the printed SQL statement is copied to the database.


Reply to discussion (solution)

Additional information: after adding the code to the code, the returned information is as follows:

That is, $ adSearchRs = $ config [DAOIMPL]-> getADSearchInfo ($ search, $ offset, $ page); an error has occurred!
Is your SQL command call audioconver. p_advert_qry ('-1?
Correct? Who is the returned result?

Commands out sync: you can't run this command new
This is the cause of "command not synchronized". if this error occurs, the client function is being called in the wrong order.

You are executing the call command without the select
Therefore, two result sets are generated.
The first is the select result set. because no select command is available, it is invalid and cannot be processed as a resource by php.
The second is the call result set.

Although mysql provides the C function mysql_next_result for moving the result set
However, the mysql extension of php does not provide this function. Therefore, it can be considered that mysql of php does not support stored procedures very well (because mysql 4 does not support stored procedures)
Mysqli extension provides the mysqli_next_result function. Therefore, if a stored procedure is used, it is best to use the mysqli driver.

Attach the C code to solve the problem

Do {result = mysql_store_result (& conn); mysql_free_result (result) ;}while (! Mysql_next_result (& conn); // to solve the "2014: Commands out of sync;" problem the code above must be called after each query when multiple queries are executed.

This is not accurate enough, but it means
The mysql extension will automatically select a non-empty result set, but the mysql_free_result function can only clear the first result set. In this way, the call result set cannot be cleared, so an error occurs.

Commands out sync: you can't run this command new
This is the cause of "command not synchronized". if this error occurs, the client function is being called in the wrong order.

You are executing the call command without the select
Therefore, two result sets are generated.
The first is the select result set. because no select command is available, it is invalid and cannot be processed as a resource by php.
The second is the call result set.

Although mysql provides the C function mysql_next_result for moving the result set
However, the mysql extension of php does not provide this function. Therefore, it can be considered that mysql of php does not support stored procedures very well (because mysql 4 does not support stored procedures)
Mysqli extension provides the mysqli_next_result function. Therefore, if a stored procedure is used, it is best to use the mysqli driver.

Attach the C code to solve the problem

Do {result = mysql_store_result (& conn); mysql_free_result (result) ;}while (! Mysql_next_result (& conn); // to solve the "2014: Commands out of sync;" problem the code above must be called after each query when multiple queries are executed.




This is also true on the Internet, but mysql does not have this method. it should be mysqli_store_result.

Mysql_close ().

Mysql_close ().


After the first query, what is the data? Disconnected? Is a connection created for each query?

Yes, after each stored procedure is executed and data is read
$ AdSearchRs = $ config [DAOIMPL]-> getADSearchInfo ($ search, $ offset, $ page );
$ AdSearchList = rs2Array ($ adSearchRs );
Mysql_close ();

So you may need to modify the database class

No, it should be
$ AdRs = $ config [DAOIMPL]-> adSearchCount ($ search );
// $ AdList = rs2Array ($ adRs );
$ AdList = mysql_fetch_array ($ adRs );
// Mysql_free_result ($ adRs );
Mysql_close ();

No, it should be
$ AdRs = $ config [DAOIMPL]-> adSearchCount ($ search );
// $ AdList = rs2Array ($ adRs );
$ AdList = mysql_fetch_array ($ adRs );
// Mysql_free_result ($ adRs );
Mysql_close ();



Close connection and reconnect data? This problem still cannot be solved. The best way is to change one of the two stored procedures on the page to SQL to indirectly solve this problem.

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.