PHP MySQL data paging

SQL SELECT statement to query always could lead to thousands of records. But the results show that all is not a good idea on a page. Therefore, we can according to the requirements of this result is divided into multiple pages. Paging means showing your query results in multiple pages, rather than just put them all in one long page. By using MySQL LIMIT clause help generate tab This clause takes two parameters. The first argument to OFFSET, the second argument should be the number of records returned by the database. Here is an LIMIT clause for records to generate a simple example of paging.

< HTML > 
< head > 
        < title > PHP Page </ title > 
</ head > 
< body > 
<? PHP 
         $ dbhost = 'localhost'; // database host 
         $ dbuser = 'root'; // username 
         $ dbpass = '123456'; // password 
         $ rec_limit = 10; // data page 10 
         $ Conn = mysqli_connect ($ dbHost, dbuser $, $ dbpass); 
         IF ($ Conn!) { 
            Die ( "connection failed: '. mysqli_error ()); 
         } 
         mysqli_select_db ($ Conn, 'Test');// data to be manipulated 
         / * Get all of the number of records * /
         $sql = "SELECT COUNT(id) FROM test ";
         $retval = mysqli_query( $conn, $sql );
         if(! $retval ) {
            die('没有获取到数据: ' . mysqli_error($conn));
         }
         $row = mysqli_fetch_array($retval, MYSQLI_NUM );
         $rec_count = $row[0];
         if( isset($_GET['page'] ) ) {
            $page = $_GET['page'] + 1;
            $offset = $rec_limit * $page ;
         }else {
            $page = 0;
            $offset = 0;
         }
         $left_rec = $rec_count - ($page * $rec_limit);
         $sql = "SELECT name ".
            "FROM test ".
            "LIMIT $offset, $rec_limit";
         $retval = mysqli_query( $conn, $sql );
         if(! $retval ) {
            die('不能获取到数据: ' . mysqli_error($conn));
         }
         while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
            echo "TEST name :{$row['name']}  <br> ";
         }
         if( $page > 0 ) {
            $last = $page - 2;
            echo "<a href = \"".$_SERVER['PHP_SELF']."?page=$last\">上一页</a> |";
            echo "<a href = \"".$_SERVER['PHP_SELF']."?page=$page\">下一页</a>";
         }else if( $page == 0 ) {
            echo "<a href = \"".$_SERVER['PHP_SELF']."?page=$page\">下一页</a>";
         }else if( $left_rec < $rec_limit ) {
            $last = $page - 2;
            echo "<a href =  \"".$_SERVER['PHP_SELF']."?page=$last\">最后一页</a>";
         }
         mysqli_close($conn);
      ?>
</body>
</html>

 

Modify the data displayed per page view data or insert multiple effects, PHP MySQL inserting data .

Guess you like

Origin www.cnblogs.com/jc2182/p/11577302.html