Home  >  Article  >  Backend Development  >  PHP MySql paging example

PHP MySql paging example

jacklove
jackloveOriginal
2018-05-22 10:09:381407browse

The following example demonstrates PHP querying data through the Mysql database and performing paging:

<?php 
$num_rec_per_page=10;   // 每页显示数量
mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);  // 数据库连接
mysql_select_db(&#39;apex1&#39;);  // 数据库名
if (isset($_GET["page"])) { 
$page  = $_GET["page"]; } 
else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page;
 $sql = "SELECT * FROM student LIMIT $start_from, $num_rec_per_page";
  $rs_result = mysql_query ($sql); // 查询数据?> 
  <table><tr><td>Name</td><td>Phone</td></tr><?php 
while ($row = mysql_fetch_assoc($rs_result)) { ?> 
            <tr>
            <td><?php echo $row[&#39;Name&#39;]; ?></td>
            <td><?php echo $row[&#39;Phone&#39;]; ?></td>            
            </tr><?php 
}; ?> </table><?php 
$sql = "SELECT * FROM student"; $rs_result = mysql_query($sql); //查询数据
$total_records = mysql_num_rows($rs_result);  // 统计总共的记录条数
$total_pages = ceil($total_records / $num_rec_per_page);  // 计算总页数
echo "<a href=&#39;pagination.php?page=1&#39;>".&#39;|<&#39;."</a> "; // 第一页
for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href=&#39;pagination.php?page=".$i."&#39;>".$i."</a> "; }; 
            echo "<a href=&#39;pagination.php?page=$total_pages&#39;>".&#39;>|&#39;."</a> "; // 最后一页?>

This article introduces PHP MySql paging examples. For more related knowledge, please pay attention to the php Chinese website.

Related recommendations:

Processing Protocol Buffers data in PHP

About PHP express query class

php sends XML data through curl and gets XML data

The above is the detailed content of PHP MySql paging example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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