mysql> use mysqlDatabase changedmysql> desc innodb_table_stats;+--------------------------+---------------------+------+-----+-------------------+-----------------------------+| Field | Type| Null | Key | Default| Extra |+--------------------------+---------------------+------+-----+-------------------+-----------------------------+| database_name| varchar(64)| NO | PRI | NULL||| table_name| varchar(64)| NO | PRI | NULL||| last_update| timestamp| NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP || n_rows | bigint(20) unsigned | NO | | NULL||| clustered_index_size | bigint(20) unsigned | NO | | NULL||| sum_of_other_index_sizes | bigint(20) unsigned | NO | | NULL||+--------------------------+---------------------+------+-----+-------------------+-----------------------------+预存数据
mysql> select * from innodb_table_stats;+-----------------+----------------+---------------------+--------+----------------------+--------------------------+| database_name | table_name | last_update| n_rows | clustered_index_size | sum_of_other_index_sizes |+-----------------+----------------+---------------------+--------+----------------------+--------------------------+| fams| admin| 2016-07-19 14:47:02 | 3 | 1 | 0 || fams| assets_in | 2016-07-14 14:42:44 | 2 | 1 | 3 || fams| assets_out | 2016-07-14 20:14:31 | 4 | 1 | 3 || fams| class| 2016-07-14 14:36:02 | 3 | 1 | 0 || fams| dog| 2016-08-11 15:25:50 | 4 | 1 | 0 || fams| fixed_assets | 2016-07-14 15:55:09 | 6 | 1 | 2 || fams| sub_class | 2016-07-14 14:38:51 | 8 | 1 | 1 || fams| user| 2016-07-14 14:15:59 | 2 | 1 | 0 || mysql| gtid_executed | 2016-07-14 12:50:25 | 0 | 1 | 0 || privilegesystem | privilege | 2016-08-08 08:56:21 | 3 | 1 | 0 || privilegesystem | role| 2016-08-08 08:26:56 | 2 | 1 | 0 || privilegesystem | role_privilege | 2016-08-08 09:51:04 | 2 | 1 | 1 || privilegesystem | user| 2016-08-08 11:07:35 | 2 | 1 | 0 || privilegesystem | user_role | 2016-08-08 11:08:15 | 2 | 1 | 2 || sys| sys_config | 2016-07-14 12:50:30 | 6 | 1 | 0 || test| datetest | 2016-07-19 10:02:38 | 2 | 1 | 0 |+-----------------+----------------+---------------------+--------+----------------------+--------------------------+16 rows in set (0.00 sec)PHP拓展准备
// 获取总的记录数$sql_total_records = "select count(*) from innodb_table_stats";$total_records_result = mysql_query($sql_total_records);$total_records = mysql_fetch_row($total_records_result);echo "总的记录数位: ".$total_records[0]."<br>";当前页
// 通过GET方式获得客户端访问的页码$current_page_number = isset($_GET["page_number"])?$_GET["page_number"]:1;if($current_page_number<1) { $current_page_number =1;}if($current_page_number>$total_pages){ $current_page_number = $total_pages;}echo "要访问的页码为:".$current_page_number;分页核心
// 获取到了要访问的页面以及页面大小,下面开始分页$begin_position = ($current_page_number-1)*$page_size;$sql = "select * from innodb_table_stats limit $begin_position,$page_size";$result = mysql_query($sql);就这样我们就可以拿到自己想要的结果集了。接下来的就是如何在页面上进行展示而已。
// 处理结果集echo "<table border="#CCF solid 1px"><th>Mysql Fixed Assets Table</th>";echo "<tr><td>DbName</td><td>TableName</td><td>Last_update</td><td>n_Nows</td><td>Clustered_Index_Size</td><td>Sum_od_Other_Index_sizes</td></tr>";while(($row = mysql_fetch_row($result))){ echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>".$row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td>".$row[3]."</td>"; echo "<td>".$row[4]."</td>"; echo "<td>".$row[5]."</td>"; echo "</tr>";}echo "</table>";// 循环显示总页数?><?phpecho "<a href="SlicePage.php?page_number=1">首页</a> ";for($i=1;$i<=$total_pages;$i++){ echo "<a href="./SlicePage.php?page_number=".$i."">第".$i."页</a> "; }echo "<a href="SlicePage.php?page_number=".($current_page_number-1)."">上一页</a> ";echo "<a href="SlicePage.php?page_number=".($current_page_number+1)."">下一页</a> ";echo "<a href="SlicePage.php?page_number=".($total_pages)."">尾页</a> ";分页实现
<meta charset="utf-8"><?php//解决中文乱码,发现不能奏效,则考虑MySQL客户端乱码情况header("Content-type=text/html;charset=utf-8");$host = "localhost";$username = "root";$password = "mysql";$dbname = "mysql";// 开始获取数据库连接$conn = mysql_connect($host,$username,$password) or die(mysql_error());// 手动更改客户端编码mysql_query("set names utf8");// 选择使用哪一个数据库mysql_select_db($dbname);// 获取总的记录数$sql_total_records = "select count(*) from innodb_table_stats";$total_records_result = mysql_query($sql_total_records);$total_records = mysql_fetch_row($total_records_result);echo "总的记录数位: ".$total_records[0]."<br>";// 获得总页数,一般来说页面大小事固定的,所以这里暂且定为一页5个数据$page_size = 3;$total_pages = ceil($total_records[0]/$page_size);echo "总页数为: ".$total_pages;// 通过GET方式获得客户端访问的页码$current_page_number = isset($_GET["page_number"])?$_GET["page_number"]:1;if($current_page_number<1) { $current_page_number =1;}if($current_page_number>$total_pages){ $current_page_number = $total_pages;}echo "要访问的页码为:".$current_page_number;// 获取到了要访问的页面以及页面大小,下面开始分页$begin_position = ($current_page_number-1)*$page_size;$sql = "select * from innodb_table_stats limit $begin_position,$page_size";$result = mysql_query($sql);// 处理结果集echo "<table border="#CCF solid 1px"><th>Mysql Fixed Assets Table</th>";echo "<tr><td>DbName</td><td>TableName</td><td>Last_update</td><td>n_Nows</td><td>Clustered_Index_Size</td><td>Sum_od_Other_Index_sizes</td></tr>";while(($row = mysql_fetch_row($result))){ echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>".$row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td>".$row[3]."</td>"; echo "<td>".$row[4]."</td>"; echo "<td>".$row[5]."</td>"; echo "</tr>";}echo "</table>";// 循环显示总页数?><?phpecho "<a href="SlicePage.php?page_number=1">首页</a> ";for($i=1;$i<=$total_pages;$i++){ echo "<a href="./SlicePage.php?page_number=".$i."">第".$i."页</a> "; }echo "<a href="SlicePage.php?page_number=".($current_page_number-1)."">上一页</a> ";echo "<a href="SlicePage.php?page_number=".($current_page_number+1)."">下一页</a> ";echo "<a href="SlicePage.php?page_number=".($total_pages)."">尾页</a> ";// 释放数据连接资源mysql_free_result($result);mysql_close($conn);?>结果初始页为:
点击页码
下一页
总结
分页是一个很实用的技术,相比较于Java的实现,PHP实现起来灵活性还是很高的。解放了繁琐的面向对象的编程,在思路清晰的时候确实能给人以美感。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。