PHP displays mysql database content by PAGE and mysql by PAGE
<? Php $ conn = mysql_connect ('2017. 0.0.1 ', 'root', ''); mysql_query ('use test', $ conn); mysql_query ('set names utf8', $ conn); $ perNumber = 3; // number of records displayed on each page $ page = $ _ GET ['page']; // obtain the current page value $ count = mysql_query ("select count (*) from kangbiao "); // obtain the total number of records $ rs = mysql_fetch_array ($ count); $ totalNumber = $ rs [0]; $ totalPage = ceil ($ totalNumber/$ perNumber ); // calculate the total number of pages if (! Isset ($ page) {$ page = 1;} // if no value exists, 1 $ startCount = ($ page-1) * $ perNumber; // start with paging, calculate the Starting record $ result = mysql_query ("select * from kangbiao limit $ startCount, $ perNumber") according to this method "); // calculate the start record and number of records based on the previous calculation echo "<table border = '1'>"; echo "<tr> "; echo "<th> id </th>"; echo "<th> name </th>"; echo "<th> age </th> "; echo "<th> grade </td>"; echo "</tr>"; while ($ row = mysql_fetch_array ($ result) {echo "<tr> "; echo "<td> $ row [0] </td>"; ec Ho "<td> $ row [1] </td>"; echo "<td> $ row [2] </td> "; echo "<td> $ row [3] </td>"; // display the database content echo "</tr>" ;}echo "</table> "; if ($ page! = 1) {// the page number is not equal to 1?> <A href = "02.php? Page = <? Php echo $ page-1;?> "> Previous page </a> <! -- Display previous page --> <? Php} for ($ I = 1; $ I <= $ totalPage; $ I ++) {// page displayed cyclically?> <A href = "02.php? Page = <? Php echo $ I;?> "> <? Php echo $ I;?> </A> <? Php} if ($ page <$ totalPage) {// if the page is smaller than the total number of pages, the next page Link is displayed. <A href = "02.php? Page = <? Php echo $ page + 1;?> "> Next page </a> <? Php }?>
Running result:
How does php display records in the mysql database by page? Thanks
$ PageSize = 20; // defines the number of entries displayed on each page
$ Page = isset ($ _ GET ['page'])? Intval ($ _ GET ['page']): 1; // GET the current page number
If ($ page <1 ){
$ Page = 1;
}
$ Tsql = "select count (*) as count from biao order by id desc ";
$ Result = mysql_query ($ tsql );
$ Record = mysql_fetch_array ($ result );
$ Count = $ record ['Count']; // The total number of records here
// Total number of pages
$ PageCount = ceil ($ count/$ pageSize );
/* Check the page again */
If ($ page> $ pageCount ){
$ Page = $ pageCount;
}
// Calculate the start count
$ Start = ($ page-1) * $ pageSize;
// Retrieve paging data
$ SQL = "select * from biao order by id desc limit $ start, $ pageSize ";
The following code is okay, just like yours.
How can I retrieve data from a php database by page?
<? Php
Include ("connection. php ");
$ PerNumber = 10; // number of records displayed on each page
$ Page = $ _ GET ['page']; // obtain the current page value.
$ Count = mysql_query ("select count (*) from user"); // obtain the total number of records
$ Rs = mysql_fetch_array ($ count );
$ TotalNumber = $ rs [0];
$ TotalPage = ceil ($ totalNumber/$ perNumber); // calculate the total number of pages
If (! Isset ($ page )){
$ Page = 1;
} // If no value exists, 1 is assigned.
$ StartCount = ($ page-1) * $ perNumber; // start by page. Calculate the start record based on this method.
$ Result = mysql_query ("select * from user limit $ startCount, $ perNumber"); // calculate the start record and number of records based on the previous calculation.
While ($ row = mysql_fetch_array ($ result )){
Echo "user_id:". $ row [0]. "<br> ";
Echo "username:". $ row [1]. "<br>"; // displays the database content.
}
If ($ page! = 1) {// the page number is not equal to 1
?>
<A href = "fenye. php? Page = <? Php echo $ page-1;?> "> Previous page </a> <! -- Display previous page -->
<? Php
}
For ($ I = 1; $ I <= $ totalPage; $ I ++) {// The page is displayed cyclically.
?>
<A href = "fenye. php? Page = <? Php echo $ I;?> "> <? Php echo $ I;?> </A>
<? Php
}
If ($ page <$ totalPage) {// if the page is smaller than the total number of pages, the next page Link is displayed.
?>
<A href = "fenye. php? Page = <? Php echo $ page + 1;?> "> Next page </a>
<? Php
}
?>
======================================
This is very simple... and I wrote comments... I don't know if it doesn't suit you ..