Php+mysql Paging problem

Source: Internet
Author: User
Tags php mysql
Ask: Php+mysql pagination, first post code, then describe the problem
Code:

$page =isset ($_get[' page ')? Intval ($_get[' page ')): 1; This is to get the value of page in Page=18, if there is no page, then the number of pages is 1. $searchinfo =isset ($_get[' search ')? ($_get[' Search ']): "1";                                      $search = ($searchinfo = = ' Please enter keywords ')?-1: $searchinfo; $num = 1; 3 data per page */* First we want to get how much data in the database, in order to determine the specific number of pages, the specific formula is the total database divided by the number of bars displayed per page, more than one. That is to say, 10/3=3.3333=4 will be in one. */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) {$total = $adList [0][' cn ']; Query all data}else{$total = 0;}                                    $url = ' views/adsearchresult.php ';//Get url//page number calculation $pagenum=ceil ($total/$num) on this page; Get the total number of pages, also the last page $page=min ($pagenum, $page);//Get home $prepg= $page -1;//prev $nextpg= ($page = = $pagenum ?                                        0: $page + 1);//Next page $offset= ($page-1) * $NUM; Gets the value of the first parameter of the limit, assuming that the first page is (1-1) *10=0, and the second page is (2-1) *10=10.  Start paging navigation bar code: $pagenav = "Show". ($total? ($offset + 1): 0). "-". Min ($offset +10, $total)."Records, Total $total records ";//If only one page jumps out of the function://if ($pagenum <=1) return false; $pagenav. =" Home "; if ($PREPG) $pagenav. =" front page "; else $pagenav. = "front page", if ($NEXTPG) $pagenav. = "Back Page"; else $pagenav. = "Back page"; $pagenav. = "Last"; $pagenav. = "Page, total $pagenum pages ";//If the number of pages passed in is greater than the total number of pages, the error message is displayed 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 [' Subject ']. '       '. $it [' Pic_url ']. '       '. $it [' Advert_url ']. '       '. $it [' If_valid ']. ' ';} Display data echo $pagenav;//Output paging navigation



Problem Description: Paging idea: First calculate the number of records of the query criteria, and then based on the page number and the number of pages per page to calculate the paging data (there are two of pages).
The first query, which is OK, can return the number of data bars that satisfy the condition ($adRs = $config [Daoimpl]->adsearchcount ($search);) but the second query executes and the error message is:
The corresponding code is the while ($it =mysql_fetch_array ($adSearchRs)) is the time to get the results returned. In addition, the printed SQL copy to the database is returned with data


Reply to discussion (solution)

Supplemental information: After adding the code in the code, the information returned is as follows

That is $adSearchRs = $config [Daoimpl]->getadsearchinfo ($search, $offset, $page); It's gone wrong!
Is your SQL command call Audioconver.p_advert_qry ('-1 ', 0, 1)?
Is it correct? Who were the results returned to?

Commands out Sync:you can ' t run the This command new
This is the reason why the command is not synchronized, and if you encounter this error, you are calling the client function in the wrong order

You're doing a call command, no SELECT.
So two result sets will be generated
The first is the result set of SELECT, because there is no select command, so it is invalid and cannot be processed into a resource by PHP
The second is the result set of call

Although MySQL provides a C function Mysql_next_result for moving result sets
However, PHP's MySQL extension does not provide this function, so it is considered that PHP MySQL does not support stored procedures well (because MYSQL4 does not support stored procedures)
The mysqli extension provides the Mysqli_next_result function, so 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    it is necessary to invoke the above code after each query when executing multiple queries.

I just said it wasn't accurate, but it means it's over.
The MySQL extension should automatically select a non-empty result set, but the Mysql_free_result function can only empty the first result set. So the result set of call was not emptied, so something went wrong.

Commands out Sync:you can ' t run the This command new
This is the reason why the command is not synchronized, and if you encounter this error, you are calling the client function in the wrong order

You're doing a call command, no SELECT.
So two result sets will be generated
The first is the result set of SELECT, because there is no select command, so it is invalid and cannot be processed into a resource by PHP
The second is the result set of call

Although MySQL provides a C function Mysql_next_result for moving result sets
However, PHP's MySQL extension does not provide this function, so it is considered that PHP MySQL does not support stored procedures well (because MYSQL4 does not support stored procedures)
The mysqli extension provides the Mysqli_next_result function, so 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    it is necessary to invoke the above code after each query when executing multiple queries.




That's what the internet says, but MySQL doesn't have this method, it should be mysqli_store_result.

Mysql_close () just a little bit.

Mysql_close () just a little bit.


Disconnect the data after the first query? Create a connection every time the query is made?

Yes, after each execution of the stored procedure and reading the data
$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 the connection, reconnect the data? Still can not solve this problem, the most there is no way to change one of the two stored procedures in the page to SQL, only indirectly solve the problem.
  • Related Article

    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.