Correction status:qualified
Teacher's comments:
connect.php
<?php //pdo数据库连接配置文件 $dbname = 'test'; $type = 'mysql'; $host='127.0.0.1'; $charset = 'utf8'; $port = '3306'; $user= 'root'; $pass = 'root'; $pdo = connect($dbname,$type,$host,$charset,$port,$user,$pass); ?>
点击 "运行实例" 按钮查看在线实例
func_pdo.php
<?php //function_exists(function_name) 检测函数是否被定义 if (!function_exists('connect')) { /* * @param $dbname * @param $type * @param $host * @param $charset * @param $port * @param $user * @param $pass * @return PDO */ function connect($dbname, $type, $host, $charset, $port, $user, $pass){ //数据源 $dsn = "{$type}:host={$host};dbname={$dbname};charset={$charset};port={$port}"; //配置连接属性 $options= [ PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,//设置错误模式 PDO::ATTR_CASE=>PDO::CASE_NATURAL,//设置数据库字段保持不变 PDO::ATTR_EMULATE_PREPARES=>TRUE,//启用pdo模拟功能 PDO::ATTR_PERSISTENT=>TRUE//启用持久性连接 ]; //连接调试代码段 try{ //实例化pdo对象 $pdo = new PDO($dsn,$user,$pass,$options); // echo '连接成功'; }catch(PDOException $e){ //抛出异常 die('ERROR:'.$e->getMessage()); } return $pdo; } } //新增数据 if(!function_exists('insert')){ /* * @param $pdo * @param $table * @param array $data * @return bool */ function insert($pdo, $table, $data=[]){ // 创建sql预处理语句 $sql = "INSERT IGNORE {$table} SET "; foreach(array_keys($data) as $fileld){ $sql .= $fileld.'=:'.$fileld.', '; } //去除sql语句的左右空格 并去除右边的逗号 $sql = rtrim(trim($sql),',').';'; //创建pdo预处理对象 $stmt = $pdo->prepare($sql); //绑定参数到预处理对象 foreach($data as $fileld => $value){ $stmt->bindValue(":{$fileld}",$value); } //执行新增操作 if($stmt->execute()){ if($stmt->rowCount()>0){ return true; } }else{ return false; } } } //更新数据 if(!function_exists('updata')){ /* * @param $pdo * @param $table * @param array $data * @param string $where * @return bool */ function update($pdo, $table, $data=[], $where=''){ // 创建sql语句 $sql = "UPDATE {$table} SET "; foreach (array_keys($data) as $field) { $sql .= $field.'=:'. $field.', '; } //去掉尾部的逗号并添加分号 $sql = rtrim(trim($sql),', '); // // 添加更新条件 if (!empty($where)) { $sql .= ' WHERE '.$where.';'; } else { exit('条件不能为空'); } // 创建stmt对象 $stmt = $pdo->prepare($sql); //绑定参数到预处理对象中 foreach ($data as $field => $value) { $stmt->bindValue(":{$field}", $value); } //执行更新操作 if ($stmt->execute()) { if ($stmt->rowCount()>0) { return true; } } else { return false; } }; }; //查询单条数据 if (!function_exists('find')){ // 查询单条数据 function find($pdo,$table,$fields,$where){ // c创建sql语句 $sql = 'SELECT '; if(is_array($fields)){ foreach($fields as $field){ $sql .= $field.', '; } }else{ $sql .=$fields; } $sql = rtrim(trim($sql),', '); $sql .= ' FROM '.$table; //添加查询条件 if(!empty($where)){ $sql .= " WHERE ".$where; } $sql .= ' LIMIT 1'; $sql = rtrim(trim($sql),', ').';'; //创建stmt对象 $stmt = $pdo->prepare($sql); //执行查询操作 if ($stmt->execute()){ if ($stmt->rowCount()>0){ $stmt->setFetchMode(PDO::FETCH_ASSOC); return $stmt->fetch(); } }else{ return $stmt->error; } }; } //查询多条数据 if (!function_exists('select')){ //查询多条记录 /* * @param $pdo * @param $table * @param $fields * @param string $where * @param string $order * @return bool */ function select($pdo, $table, $fields, $where='', $order='', $limit){ // 创建sql语句 $sql = 'SELECT '; if (is_array($fields)){ foreach($fields as $field){ $sql .= $field.', '; } }else{ $sql .=$fields; } $sql = rtrim(trim($sql),','); $sql .=' FROM '.$table; //添加查询条件 if(!empty($where)) { $sql .= ' WHERE '. $where; } if(!empty($limit)) { $sql .= ' LIMIT '. $limit; } //添加排序条件 if (!empty($order)){ $sql .=' ORDER BY '.$order; } //去掉尾部的逗号并添加逗号 $sql = rtrim(trim($sql),' , ').';'; //创建pdo预处理对象 $stmt= $pdo->prepare($sql); //执行操作 if ($stmt->execute()){ if($stmt->rowCount()>0){ $stmt->setFetchMode(PDO::FETCH_ASSOC); return $stmt->fetchAll(); } }else{ return false; } }; } //删除数据 if (!function_exists('delete')) { /* * 删除记录 * @param $pdo * @param $table * @param string $where * @return bool */ function delete($pdo,$table, $where='') { //创建SQL语句 $sql = "DELETE FROM {$table} "; //添加删除条件 if(!empty($where)) { $sql .= 'WHERE '. $where; }else{ exit('条件不能为空'); } //去掉尾部逗号,并添加分号结束 $sql = rtrim(trim($sql),', ').';'; //创建PDO预处理对象 $stmt = $pdo->prepare($sql); //执行删除操作 if($stmt->execute()){ if($stmt->rowCount()>0){ return true; } } else { return false; } } } ?>
点击 "运行实例" 按钮查看在线实例
page.php
<?php //连接数据库获取表中的全部信息 require 'lib/pdo_function.php'; require 'lib/config.php'; //GET获取当前页数 $page = isset($_GET['p'])?$_GET['p']:1; //当页数等于0的时候赋值第一页 $page = ($page==0)?1:$page; //定义分页每页显示数量 $num = 5; //页数偏移量 $offset = ($page-1)*$num; //总页数的计算 $table = 'blast_members'; $fields = ' * '; $pages = ceil(count(select($pdo, $table, $fields, $where='', $order='',$limit=''))/$num); $page = ($page==$pages)?$pages-1:$page; //表名 $table = 'blast_members'; //调用查询函数 $fields=['uid','username','qq','name','coin']; $limit ="{$offset},{$num}"; $rows = select($pdo, $table, $fields, $where='', $order='',$limit); ?> <!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"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" type="text/css" href="../bootstrap/css/bootstrap.css"/> <script src="../bootstrap/js/bootstrap.js" type="text/javascript" charset="utf-8"></script> <title>分页的方法</title> <style type="text/css"> th{text-align:center;} .form-control{ width: 60px; display: inline-block; } .inline{ position: relative; } .inline nav{ position: absolute; top:0; left:50%; margin-left: -15%; } .inline form{ position: relative; top:-59px; left:102%; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12 text-center"> <h1>用户列表</h1> <table class="table table-bordered table-hover" > <tr class="info" > <th>用户ID</th> <th>用户名</th> <th>联系方式</th> <th>用户昵称</th> <th>账户余额</th> <th>操作</th> </tr> <?php foreach ($rows as $row):?> <tr> <td><?php echo $row['uid']?></td> <td><?php echo $row['username']?></td> <td><?php echo $row['qq']?></td> <td><?php echo $row['name']?></td> <td><?php echo $row['coin']?></td> <td><a class="btn btn-info btn-sm" href="">编辑</a> <a class="btn btn-danger btn-sm" href="">删除</a></td> </tr> <?php endforeach;?> </table> </div> <div class="col-md-12 inline"> <nav aria-label="..."> <ul class="pagination"> <li><a href="<?php echo "http://{$_SERVER['HTTP_HOST']}/0427/page-1.php?p=1"?>">首页 </a></li> <li><a href="<?php echo "http://{$_SERVER['HTTP_HOST']}/0427/page-1.php?p=".($page-1) ?>" aria-label="Previous"><span aria-hidden="true">«</span></a></li> <?php for($i=1;$i<=$pages;$i++): ?> <li class="<?php if($_GET['p']==$i){echo 'active';} ?>"><a href="<?php echo "http://{$_SERVER['HTTP_HOST']}/0427/page-1.php?p={$i}" ?>"><?php echo $i;?> </a></li> <?php endfor;?> <li><a href="<?php echo "http://{$_SERVER['HTTP_HOST']}/0427/page-1.php?p=".($page+1); ?>" aria-label="Previous"><span aria-hidden="true">»</span></a></li> <li><a href="<?php echo "http://{$_SERVER['HTTP_HOST']}/0427/page-1.php?p=".$page; ?>">尾页 </a></li> </ul> <form action="" method="get"> <select name="p" class="form-control"> <?php for($i=1;$i<=$pages;$i++): ?> <option value="<?php echo $i; ?>"<?php if ($_GET['p']==$i){echo 'selected';} ?>><?php echo $i; ?></option> <?php endfor;?> </select> <input type="submit" value="跳转"/> </form> </nav> </div> </div> </div> </body> </html>
点击 "运行实例" 按钮查看在线实例