Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize data pagination with PHP MySQL

Shulou Source: shulou.com Published: 2022-06-03 18:54:51 10月02日 Update

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!

Tags: Data pages parameters clauses results learning content multiple components databases queries two ideas hosts multiple passwords that is ideas situations meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft Apple Shulou Information Huawei