PHP MySql 分页实例

 1 <?php 
 2 $num_rec_per_page=10;   // 每页显示数量
 3 mysql_connect('localhost','root','');  // 数据库连接
 4 mysql_select_db('apex1');  // 数据库名
 5 if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
 6 $start_from = ($page-1) * $num_rec_per_page; 
 7 $sql = "SELECT * FROM student LIMIT $start_from, $num_rec_per_page"; 
 8 $rs_result = mysql_query ($sql); // 查询数据
 9 ?> 
10 <table>
11 <tr><td>Name</td><td>Phone</td></tr>
12 <?php 
13 while ($row = mysql_fetch_assoc($rs_result)) { 
14 ?> 
15             <tr>
16             <td><?php echo $row['Name']; ?></td>
17             <td><?php echo $row['Phone']; ?></td>            
18             </tr>
19 <?php 
20 }; 
21 ?> 
22 </table>
23 <?php 
24 $sql = "SELECT * FROM student"; 
25 $rs_result = mysql_query($sql); //查询数据
26 $total_records = mysql_num_rows($rs_result);  // 统计总共的记录条数
27 $total_pages = ceil($total_records / $num_rec_per_page);  // 计算总页数
28 
29 echo "<a href='pagination.php?page=1'>".'|<'."</a> "; // 第一页
30 
31 for ($i=1; $i<=$total_pages; $i++) { 
32             echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
33 }; 
34 echo "<a href='pagination.php?page=$total_pages'>".'>|'."</a> "; // 最后一页
35 ?>

 

posted @ 2016-08-11 15:10  沙瑞金书记  阅读(212)  评论(0编辑  收藏  举报