Home Backend Development PHP Tutorial php+mysql 分页有关问题

php+mysql 分页有关问题

Jun 13, 2016 am 11:59 AM
gt lt mysql nbsp page

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles