首页 > 代码库 > mysql数据导出并分页

mysql数据导出并分页

 1 <?php
 2 @mysql_connect("localhost","root","")or die;
 3 @mysql_select_db("test1")or die;
 4 $query = @mysql_query("select * from yonghu")or die;
 5 $pagesize = 15;        //设置每页记录数
 6 $sum = mysql_numrows($query);    //计算总记录数
 7 if($sum%$pagesize == 0)
 8     $total = (int)($sum/$pagesize);
 9 else 
10     $total = (int)($sum/$pagesize)+1;
11 
12 if(isset($_GET[‘page‘]))
13 {
14     $p = (int)$_GET[‘page‘];
15 }
16 else 
17 {
18     $p = 1;
19 }
20 
21 $start = $pagesize * ($p-1);
22 $query = @mysql_query("select * from yonghu limit $start,$pagesize")or die;
23 echo "<table border=1><tr align=center><th>用户名</th><th>性别</th><th>出生日期</th><th>邮箱</th></tr>";
24 while ($row = mysql_fetch_array($query))
25 {
26     $username = $row[‘username‘];
27     $sex = $row[‘sex‘];
28     $birth = $row[‘birth‘];
29     $email = $row[‘email‘];
30     echo "<tr>";
31     echo "<td>{$username}</td>";
32     echo "<td>{$sex}</td>";
33     echo "<td>{$birth}</td>";
34     echo "<td>{$email}</td>";
35     echo "</tr>";
36 }
37 echo "<table>";
38 if ($p>1)
39 {
40     $prev = $p-1;
41     echo "<a href = http://www.mamicode.com/‘?page=$prev‘>上一页</a>";
42 }
43 if($p<$total)
44 {
45     $next = $p+1;
46     echo "<a href = http://www.mamicode.com/‘?page=$next‘>下一页</a>";
47 }
48 ?>

技术分享

 

 

该分页使用的是地址栏的$_GET方式来将值赋给下一页的<a>标签里的链接变量,从而实现了将该值通过

$start = $pagesize * ($p-1);

的关系,成为limit新的偏移量。

 1 if(isset($_GET[‘page‘]))
 2 {
 3     $p = (int)$_GET[‘page‘];
 4 }
 5 else 
 6 {
 7     $p = 1;
 8 }
 9 
10 $start = $pagesize * ($p-1);

 

mysql数据导出并分页