Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php+mysql 分页问题

php+mysql 分页问题

Jun 23, 2016 pm 01:55 PM
php+mysql Pagination

求教:php+mysql分页 ,先贴代码,再描述问题
代码:

$page=isset($_GET['page'])?intval($_GET['page']):1;        //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。$searchinfo=isset($_GET['search'])?($_GET['search']):"-1"; $search=($searchinfo=='请输入关键字')?-1:$searchinfo;$num=1;                                      //每页显示3条数据/*首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,具体的公式就是总数据库除以每页显示的条数,有余进一。也就是说10/3=3.3333=4 有余数就要进一。*/ 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']; //查询所有的数据}else{	$total=0;}$url='views/adSearchResult.php';//获取本页URL//页码计算$pagenum=ceil($total/$num);                                    //获得总页数,也是最后一页$page=min($pagenum,$page);//获得首页$prepg=$page-1;//上一页$nextpg=($page==$pagenum ? 0 : $page+1);//下一页$offset=($page-1)*$num;                                        //获取limit的第一个参数的值,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。//开始分页导航条代码:$pagenav="显示第 <B>".($total?($offset+1):0)."</B>-<B>".min($offset+10,$total)."</B> 条记录,共 $total 条记录 ";//如果只有一页则跳出函数://if($pagenum<=1) return false;$pagenav.=" <a href=javascript:dopage('result','$url?page=1');>首页</a> ";if($prepg) $pagenav.=" <a href=javascript:dopage('result','$url?page=$prepg');>前页</a> "; else $pagenav.=" 前页 ";if($nextpg) $pagenav.=" <a href=javascript:dopage('result','$url?page=$nextpg');>后页</a> "; else $pagenav.=" 后页 ";$pagenav.=" <a href=javascript:dopage('result','$url?page=$pagenum');>尾页</a> ";$pagenav.="</select> 页,共 $pagenum 页";//假如传入的页数参数大于总页数,则显示错误信息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  '<div class="sylist1">       <input type="checkbox" class="check" name="checkbox" id="checkbox'.$it['id'].'" value="'.$it['id'].'"/>       </div>       <div class="sylist2">'.$it['title'].'</div>       <div class="sylist3">'.$it['subject'].'</div>       <div class="sylist4">'.$it['pic_url'].'</div>       <div class="sylist5">'.$it['advert_url'].'</div>       <div class="sylist6">'.$it['if_valid'].'</div>       </div>';}//显示数据echo $pagenav;//输出分页导航
Copy after login



问题描述:分页思路:先计算出查询条件的记录条数,然后根据页码和每页条数计算出分页数据(存在两次分页)。
第一次查询,是ok的,能够返回满足条件的数据条数,($adRs = $config[DAOIMPL]->adSearchCount($search););但是第二次查询执行后就报错了,报错信息是:
对应代码是While($it=mysql_fetch_array($adSearchRs)) 就是获取返回结果的时候。另外打印出的sql copy到数据库是有数据返回的


回复讨论(解决方案)

补充信息: 在代码中补充如图中代码后,返回的信息如下

那就是 $adSearchRs = $config[DAOIMPL]->getADSearchInfo($search,$offset,$page); 已经出错了!
你的 sql 指令是 call audioconver.p_advert_qry('-1',0,1) 吗?
正确吗?结果返回给谁了?

commands out sync: you can't run this command new
这是“命令不同步”的原因,如果遇到该错误,说明正在以错误顺序调用客户端函数

你执行的是 call 命令,没有 select
所以会产生两个结果集
第一个是 select 的结果集,由于没有 select 命令,所以是无效的不能 php 处理成资源
第二个才是 call 的结果集

虽然 mysql 提供了 C 函数 mysql_next_result 用于移动结果集
但 php 的 mysql 扩展并没有提供该函数,所以可认为php 的 mysql 不能很好的支持存储过程(因为 mysql4 并不支持存储过程)
mysqli 扩展提供了 mysqli_next_result 函数,所以如果使用了存储过程,最好是用 mysqli 驱动

附上解决该问题的 C 代码

 do    {         result=mysql_store_result(&conn);          mysql_free_result(result);    }while(!mysql_next_result(&conn)); // to solve the "2014:Commands out of sync; " problem    凡是在执行多查询时,每个查询后都需调用以上这段代码才行。
Copy after login
Copy after login

刚才说的不够准确,但意思是到了
应该是 mysql 扩展会自动选择非空结果集,但 mysql_free_result 函数只能清空第一个结果集。这样 call 的结果集得不到清空,所以出错了

commands out sync: you can't run this command new
这是“命令不同步”的原因,如果遇到该错误,说明正在以错误顺序调用客户端函数

你执行的是 call 命令,没有 select
所以会产生两个结果集
第一个是 select 的结果集,由于没有 select 命令,所以是无效的不能 php 处理成资源
第二个才是 call 的结果集

虽然 mysql 提供了 C 函数 mysql_next_result 用于移动结果集
但 php 的 mysql 扩展并没有提供该函数,所以可认为php 的 mysql 不能很好的支持存储过程(因为 mysql4 并不支持存储过程)
mysqli 扩展提供了 mysqli_next_result 函数,所以如果使用了存储过程,最好是用 mysqli 驱动

附上解决该问题的 C 代码

 do    {         result=mysql_store_result(&conn);          mysql_free_result(result);    }while(!mysql_next_result(&conn)); // to solve the "2014:Commands out of sync; " problem    凡是在执行多查询时,每个查询后都需调用以上这段代码才行。
Copy after login
Copy after login




网上也是这么说,但mysql 没有这个方法,应该是mysqli_store_result 吧

mysql_close() 一下就可以了

mysql_close() 一下就可以了


 在第一个查询后将数据?连接断掉?每次查询都创建一次连接?

对,在每次执行存储过程并读取数据之后
$adSearchRs = $config[DAOIMPL]->getADSearchInfo($search,$offset,$page);
$adSearchList = rs2Array($adSearchRs);
mysql_close();

所以你可能需要修改数据库类

不对,应该是
$adRs = $config[DAOIMPL]->adSearchCount($search);
//$adList = rs2Array($adRs);
$adList=mysql_fetch_array($adRs);
//mysql_free_result($adRs);
mysql_close();

不对,应该是
$adRs = $config[DAOIMPL]->adSearchCount($search);
//$adList = rs2Array($adRs);
$adList=mysql_fetch_array($adRs);
//mysql_free_result($adRs);
mysql_close();



关闭连接,重新连接数据?还是不能解决这个问题,最有没有办法就好将页面中两个存储过程中的一个改成sql,才间接解决这个问题。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to create custom pagination in CakePHP? How to create custom pagination in CakePHP? Jun 04, 2023 am 08:32 AM

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

PHP development: How to implement table data sorting and paging functions PHP development: How to implement table data sorting and paging functions Sep 20, 2023 am 11:28 AM

PHP development: How to implement table data sorting and paging functions In web development, processing large amounts of data is a common task. For tables that need to display a large amount of data, it is usually necessary to implement data sorting and paging functions to provide a good user experience and optimize system performance. This article will introduce how to use PHP to implement the sorting and paging functions of table data, and give specific code examples. The sorting function implements the sorting function in the table, allowing users to sort in ascending or descending order according to different fields. The following is an implementation form

How to use JavaScript to implement table paging function? How to use JavaScript to implement table paging function? Oct 20, 2023 pm 06:19 PM

How to use JavaScript to implement table paging function? With the development of the Internet, more and more websites use tables to display data. In some cases where the amount of data is large, the data needs to be displayed in pages to improve user experience. This article will introduce how to use JavaScript to implement table paging function and provide specific code examples. 1. HTML structure First, we need to prepare an HTML structure to host tables and paging buttons. We can use &lt;tab

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

Using JavaScript to implement paging display of table data Using JavaScript to implement paging display of table data Jun 16, 2023 am 10:00 AM

As data continues to grow, tabular display becomes more difficult. Most of the time, the amount of data in a table is so large that it becomes slow to load and users need to constantly browse the page to find the data they want. This article will introduce how to use JavaScript to realize paginated display of table data, making it easier for users to find the data they want. 1. Dynamically create tables. In order to make the paging function more controllable, tables need to be created dynamically. In the HTML page, add a table element similar to the one below.

Vue component practice: paging component development Vue component practice: paging component development Nov 24, 2023 am 08:56 AM

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

How to implement paging function in Vue technology development How to implement paging function in Vue technology development Oct 09, 2023 am 09:06 AM

Vue is a popular JavaScript framework for building user interfaces. In the development of Vue technology, implementing paging function is a common requirement. This article will introduce how to use Vue to implement paging function and provide specific code examples. Before we start, we need to prepare some basic knowledge in advance. First, we need to understand the basic concepts and syntax of Vue. Secondly, we need to know how to use Vue components to build our application. Before we start, we need to install a paging plug-in in the Vue project,

Best way to implement array pagination in PHP Best way to implement array pagination in PHP May 04, 2024 pm 02:39 PM

There are two most common ways to paginate PHP arrays: using the array_slice() function: calculate the number of elements to skip, and then extract the specified range of elements. Use built-in iterators: implement the Iterator interface, and the rewind(), key(), current(), next(), and valid() methods are used to traverse elements within the specified range.

See all articles