Implementation of pagination in PHP MySQL

Source: Internet
Author: User
Tags php mysql
Recently, many people in the forum asked me how to display the query results on pages. I hope the following code will help you improve your program. These codes are used for MYSQL, but can be easily transplanted to other SQL statements.

Due to the special nature of each program, I used some common statements in MYSQL queries. Replace TABLE with your TABLE name; replace YOUR_CONDITION_HERE with your condition statement; replace WHATEVER with the field name you want to sort by it (if you want to sort it in reverse order, do not forget to add the DESC clause ).

<? Php
$ Qh = mysql_query ("select count (*) AS rcnt from table where YOUR_CONDITION_HERE order by whatever ");
$ Data = mysql_fetch_array ($ qh );
$ Nr = $ data ["rcnt"];
// Determine whether the offset parameter is passed to the script. If not, use the default value 0.
If (empty ($ offset ))
$ Offset = 0;
// Query result (20 entries per page, but you can change it by yourself)
$ Result = mysql_query ("SELECT id, name, phone from table where YOUR_CONDITION_HERE order by whatever limit $ offset, 20 ");

// Display the 20 returned Records
While ($ data = mysql_fetch_array ($ result ))
// Use the code you used to display the returned record
// Next, write the link to another page
If (! $ Offset) // if the offset is 0, the link to the previous page is not displayed.
$ Preoffset = $ offset-20;
Print "<a href =" $ PHP_SELF? Offset = $ preoffset "> Previous Page </a> & nbsp ;";
// Calculate the total number of required pages
$ Pages = ceil ($ nr/20); // $ pages variable now contains the required page number
For ($ I = 1; $ I <= $ pages; $ I)
$ Newoffset = 20 * $ I;
Print "<a href =" $ PHP_SELF? Offset = $ newoffset "> $ I </a> & nbsp ;";
// Check whether it is the last page
If ($ pages! = 0 & ($ newoffset/20 )! = $ Pages)
Print "<a href =" $ PHP_SELF? Offset = $ newoffset "> Next page </a> & nbsp ;";
This just gives you an overview of how to display the query results by page. You can complete other functions by yourself.

Note: $ PHP_SELF has only one offset parameter. You can add your own items as needed. This method is inefficient at querying tables with more than one million records.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.