index.php
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <style> body {padding:100px 0 0;} .paging {text-align:center;} </style> </head> <body> <?php require 'lib/func_pdo.php'; require 'lib/fun_paging.php'; $pdo = connect('php', 'mysql', '127.0.0.1', 'utf8', '3306', 'root', 'root'); // 获取总记录数 $total = find($pdo, 'staff', 'count(*) as total'); $record = $total['total']; $pagesize = 4; // 获取总页数 $page = ceil($record / $pagesize); $p = isset($_GET["p"]) ? intval($_GET["p"]) : 1; $staffs = select($pdo, 'staff', ['staff_id', 'name', 'sex', 'age', 'salary'], '', 'staff_id desc limit ' . ($p - 1) * $pagesize . ',' . $pagesize); ?> <div class="container"> <table id="tbl" class="table table-striped table-bordered table-hover"> <tr style="text-align:center;"> <td><b>ID</b></td> <td><b>姓名</b></td> <td><b>性别</b></td> <td><b>年纪</b></td> <td><b>薪酬</b></td> </tr> <?php if (!empty($staffs)) { foreach ($staffs as $v) { ?> <tr style="text-align:center;"> <td><?php echo $v['staff_id'] ?></td> <td><?php echo $v['name'] ?></td> <td><?php echo $v['sex'] == 0 ? '男' : '女' ?></td> <td><?php echo $v['age'] ?></td> <td><?php echo $v['salary'] ?></td> </tr> <?php } } ?> </table> </div> <div class="paging"> <?php echo paging($p, $page); ?> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
func_pdo.php
实例
<?php /* * PDO数据库操作函数库 */ if (!function_exists("connect")) { /* * 数据库连接 * @param type $dbname * @param type $type * @param type $host * @param type $charset * @param type $port * @param string $user * @param string $pass */ function connect($dbname, $type = 'mysql', $host = '127.0.0.1', $charset = 'utf8', $port = '3306', $user = 'root', $pass = 'root') { $dsn = "{$type}:host={$host};dbname={$dbname};charset={$charset};port={$port}"; $user = $user; $pass = $pass; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //错误模式 PDO::ATTR_CASE => PDO::CASE_NATURAL, // 自然名称 PDO::ATTR_EMULATE_PREPARES => true, // 启用模拟功能 PDO::ATTR_PERSISTENT => true, ]; try { $pdo = new PDO($dsn, $user, $pass, $options); // echo 'connect ok!'; } catch (PDOException $e) { print '连接错误' . $e->getMessage(); die(); } return $pdo; } } if (!function_exists("insert")) { /* * 新增数据 * @param type $pdo * @param type $tabname * @param type $data */ function insert($pdo, $tabname, $data = []) { // insert news set title = :title, content = :content; $sql = "insert ignore {$tabname} set "; foreach (array_keys($data) as $v) { $sql .= $v . ' =:' . $v . ', '; } $sql = rtrim(trim($sql), ','); // die($sql); $stmt = $pdo->prepare($sql); foreach ($data as $k => $v) { $stmt->bindValue(":{$k}", $v); } if ($stmt->execute()) { if ($stmt->rowCount() > 0) { return true; } } else { return false; } } } if (!function_exists("update")) { /* * 更新数据 * @param type $pdo * @param type $tabname * @param type $data * @param type $where */ function update($pdo, $tabname, $data = [], $where = '') { // update news set title = :title, content = :content where id = 1 $sql = "update $tabname set "; foreach (array_keys($data) as $v) { $sql .= $v . " = :" . $v . ', '; } $sql = rtrim(trim($sql), ','); if (!empty($where)) { $sql .= " where " . $where; } else { exit('条件不能为空'); } $stmt = $pdo->prepare($sql); foreach ($data as $k => $v) { $stmt->bindValue(":{$k}", $v); } if ($stmt->execute()) { if ($stmt->rowCount() > 0) { return true; } } else { return false; } } } if (!function_exists("find")) { /* * 查询单条记录 * @param type $pdo * @param type $tabname * @param type $fields * @param type $where */ function find($pdo, $tabname, $fields, $where = '') { $sql = "select "; if (is_array($fields)) { foreach ($fields as $v) { $sql .= $v . ','; } } else { $sql .= $fields . ','; } $sql = rtrim(trim($sql), ','); $sql .= " from $tabname "; if (!empty($where)) { $sql .= " where " . $where; } $sql .= ' limit 1'; $stmt = $pdo->prepare($sql); if ($stmt->execute()) { if ($stmt->rowCount() > 0) { // $stmt->setFetchMode(PDO::FETCH_ASSOC); return $stmt->fetch(PDO::FETCH_ASSOC); } } else { return false; } } } if (!function_exists("select")) { /* * 查询多条记录 * @param type $pdo * @param type $tabname * @param type $fields * @param type $where * @param type $order * @return boolean */ function select($pdo, $tabname, $fields, $where = '', $order = '') { $sql = "select "; if (is_array($fields)) { foreach ($fields as $v) { $sql .= $v . ','; } } else { $sql .= $fields . ','; } $sql = rtrim(trim($sql), ','); $sql .= " from $tabname "; if (!empty($where)) { $sql .= " where " . $where; } if (!empty($order)) { $sql .= " order by " . $order; } $stmt = $pdo->prepare($sql); // die($stmt->queryString); 查看sql语句 if ($stmt->execute()) { if ($stmt->rowCount() > 0) { // $stmt->setFetchMode(PDO::FETCH_ASSOC); return $stmt->fetchAll(PDO::FETCH_ASSOC); } } else { return false; } } } if (!function_exists("delete")) { /* * 删除一条记录 * @param type $pdo * @param type $tabname * @param type $where */ function delete($pdo, $tabname, $where) { $sql = "delete from $tabname "; if (!empty($where)) { $sql .= "where " . $where; } else { exit('条件不能为空'); } $stmt = $pdo->prepare($sql); if ($stmt->execute()) { if ($stmt->rowCount() > 0) { return true; } } else { return false; } } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
fun_paging.php
实例
<?php /* * 分页函数 * @param type $p * @param type $page */ function paging($p, $page) { $str = "<ul class = 'pagination'>"; $str .= "<li><a href = '?p=1'>首页</a></li>"; if ($p == 1) { $str .= "<li class='disabled'><a href='javascript:;'>上一页</a></li>"; } else { $str .= "<li><a href='?p=" . ($p - 1) . "'>上一页</a></li>"; } $active = ''; for ($i = 1; $i <= $page; $i++) { if ($p == $i) { $active = 'active'; } else { $active = ''; } $str .= "<li class='{$active}'><a href='?p={$i}'>{$i}</a></li>"; } if ($p == $page) { $str .= "<li class='disabled'><a href='javascript:;'>下一页</a></li>"; } else { $str .= "<li><a href='?p=" . ($p + 1) . "'>下一页</a></li>"; } $str .= "<li><a href='?p={$page}'>尾页</a></li>"; $str .= "</ul>"; return $str; }
运行实例 »
点击 "运行实例" 按钮查看在线实例