Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Database Operation Class Code: mysqli.func.php
[PHP]View Plain copy
- <?php
- Database Connection Constants
- Define (' db_host ', ' localhost ');
- Define (' Db_user ', ' root ');
- Define (' db_pwd ', ');
- Define (' db_name ', ' guest ');
- Connecting to a database
- Function Conn ()
- {
- $conn = Mysqli_connect (Db_host, Db_user, Db_pwd, db_name);
- Mysqli_query ($conn, "set names UTF8");
- return $conn;
- }
- Get the result set
- function Doresult ($sql) {
- $result =mysqli_query (Conn (), $sql);
- return $result;
- }
- Result set to object collection
- function dolists ($result) {
- return Mysqli_fetch_array ($result, MYSQL_ASSOC);
- }
- function Totalnums ($sql) {
- $result =mysqli_query (Conn (), $sql);
- return $result->num_rows;
- }
- Close the database
- function Closedb ()
- {
- if (! Mysqli_close ()) {
- exit (' close exception ');
- }
- }
- ?>
Pagination Implementation code:
[PHP]View Plain copy
- <?php
- Include ' mysqli.func.php ';
- Total Record Count
- $sql = "Select dg_id from Tb_user";
- $totalnums = Totalnums ($sql);
- Show number of bars per page
- $fnum = 8;
- Number of pages
- $pagenum = ceil ($totalnums/ $fnum);
- Page Constants
- @$tmp = $_get[' page '];
- Prevent malicious paging
- if ($tmp > $pagenum)
- echo "<script>window.location.href= ' index.php ' </script>";
- Calculate Paging Start value
- if ($tmp = = "") {
- $num = 0;
- } Else {
- $num = ($tmp-1) * $fnum;
- }
- Query statements
- $sql = "Select Dg_id,dg_username from Tb_user ORDER by dg_id DESC LIMIT". $num. ", $fnum";
- $result = Doresult ($sql);
- Traverse output
- while (!! $rows = dolists ($result)) {
- echo $rows [' dg_id ']. " " . $rows [' Dg_username ']. "<br>";
- }
- Page link
- for ($i = 0; $i < $pagenum; $i + +) {
- echo "<a href=index.php?page=". ($i + 1). ">". ($i + 1). "</a>";
- }
- ?>
Simple to implement the paging function, the database please configure the structure
Simple implementation of PHP page code