zoukankan      html  css  js  c++  java
  • PHP学习笔记(12)分页技术

     1 <!doctype html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>用户信息</title>
     6 </head>
     7 <body>
     8     <h1>用户信息</h1>
     9     <form action="" method = 'get'>
    10         <table border="1px">
    11             <tr>
    12                 <td width='50px'>id</td>
    13                 <td width='50px'>用户名</td>
    14                 <td width='50px'>密码</td>
    15             </tr>
    16         <?php 
    17             //连接mysql服务器
    18             mysql_connect('localhost','root','123');
    19             //选择数据库
    20             mysql_select_db(test);
    21             //设置字符集
    22             mysql_query('set names utf8');
    23             $length = 10;
    24             //数据总条数
    25             $count = mysql_fetch_array(mysql_query('SELECT COUNT(*) FROM users'))[0]; 
    26             //总页码
    27             $pages = ceil($count/$length);
    28             //页码
    29             $page = $_GET['page']?$_GET['page']:1;
    30             $prepage = $page-1;
    31             //超过总页码,页码就不变了
    32             $nextpage =$page==$pages?$page:$page+1;
    33             $offset = ($page-1)*$length;//第一页(1-1)*10+1
    34             //11 //第二页(2-1)*10+1
    35             //21//第三页(3-1)*10+1
    36             //写sql语句,关键字最好大写,方便阅读
    37             $sql = "SELECT * FROM users ORDER BY id LIMIT {$offset},{$length}";
    38             //把sql发送到mysql服务器
    39             $result = mysql_query($sql);
    40             //
    41             if (!$result) {
    42                 die(mysql_error());
    43             }
    44             while ($row = mysql_fetch_assoc($result)) {
    45                 echo "<tr><td>{$row[id]}</td><td>{$row[name]}</td><td>{$row[password]}</td></tr>";
    46             }
    47             echo "<a href='sql.php?page={$prepage}'>上一页</a>|<a href='sql.php?page={$nextpage}'>下一页</a>";
    48             //释放resource型结果集$result所关联的内存
    49             mysql_free_result($result);
    50             //关闭mysql连接
    51             mysql_close();
    52          ?>
    53         </table>
    54     </form>
    55 </body>
    56 </html>
  • 相关阅读:
    Win10 开启IIS后,访问“localhost”,报错无“C:WindowsMicrosoft.NETFramework64v4.0.30319Temporary ASP.NET Files”写访问权限
    使用pip安装Python扩展包时,如何提高速度?
    Django之路由层
    第一个Django项目
    Django简介
    DIY一个Web框架
    http协议
    Web应用和Web框架
    Python递归的经典案例
    Python修改文件的两种方法
  • 原文地址:https://www.cnblogs.com/Jacklovely/p/6080737.html
Copyright © 2011-2022 走看看