Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize data pagination with PHP MySQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "PHP MySQL how to achieve data paging", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "PHP MySQL how to achieve data paging"!

SQL SELECT statement queries can always result in thousands of records. But displaying all results on one page is not a good idea. Therefore, we can divide this result into multiple pages upon request. Pagination means displaying your query results on multiple pages, rather than just putting them all on one long page. MySQL helps generate paging by using the LIMIT clause, which takes two parameters. The first parameter is OFFSET, and the second parameter is how many records should be returned from the database. The following is a simple example of using the LIMIT clause to get records to generate pagination.

$dbhost = 'localhost';//database host $dbuser = ' root'; //username $dbpass = '123456'; //password $rec_limit = 10; //10 data per page $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(!$) conn ) { die ('Connection failure: ' . mysqli_error());}mysqli_select_db($conn,'test'); //Data to operate on/* Get all records */$sql = "SELECT COUNT(id) FROM test ";$retval = mysqli_query( $conn, $sql );if(! $retval ) { die ('No data obtained: ' . mysqli_error($conn));}$row = mysqli_fetch_array($retval, MYSQLI_NUM );$rec_count = $row[0]; if( isset($_GET['page'] ) ) { $page = $_GET['page'] + 1; $offset = $rec_limit * $page ;}else { $page = 0; $offset = 0;}$left_rec = $rec_count - ($page * $rec_limit);$sql = "SELECT name ". "FROM test ". "LIMIT $offset, $rec_limit";$retval=mysqli_query( $conn, $sql );if(! $retval ) { die ('Can 't get data: ' . mysqli_error($conn)); } while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) { echo "TEST name :{$row['name']}

";} if( $page > 0 ){ $last = $page - 2; echo "Previous|"; echo "next page";}else if( $page == 0 ) { echo "next page";}else if( $left_rec < $rec_limit ){ $last = $page - 2; echo "last page";}mysqli_close($conn);

Modify the data displayed per page or insert multiple data to see the effect, PHP MySQL inserts data.

Thank you for reading, the above is "PHP MySQL how to achieve data paging" content, after the study of this article, I believe we have a deeper understanding of PHP MySQL how to achieve data paging this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report