Correction status:qualified
Teacher's comments:当你把这个作业认真写完, 对于接口编程, 应该会有一个全新的认识
上代码:
<?php namespace User; // 先定义一个接口 interface sqlAction{ //定义4个你必须要实现的方法 public function create($data); public function del($where); public function update($data,$where); public function read($where); } //然后我们新建个操作类继承它 class DB implements sqlAction{ //老办法,先定义几个属性 protected $pdo = null; protected $table; // 然后我们用构造方法直连 public function __construct($table){ //记住这里,简单的来吧 // 先建立起数据库连接,我们这里默认了数据库phpshouce $this->pdo = new \PDO('mysql:host=localhost;dbname=phpshouce', 'root', 'root'); $this->table = $table ; } // 然后我们要实现接口的4个方法 // 第一个create public function create($data){ // 字段列表 $fields = ' (username, password,nickname,status) '; // 值列表 $values = ' (:username, :password,:nickname,:status) '; // 创建SQL $sql = 'INSERT INTO ' .$this->table .$fields . ' VALUES ' . $values; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); return [ 'count' => $stmt->rowCount(), 'id' => $this->pdo->lastInsertId() ]; } //第二个del public function del($where){ // 写语句 $sql = 'DELETE FROM ' .$this->table . ' WHERE ' .$where; // 预执行 $res = $this->pdo->prepare($sql); // 执行 $res->execute(); // 返回 return $res->rowCount(); } //第三个update public function update($data, $where) { $keyArr = array_keys($data); $set = ''; foreach ($keyArr as $value) { $set .= $value . ' = :' . $value. ', '; } $set = rtrim($set, ', '); $sql = 'UPDATE ' . $this->table. ' SET ' . $set . ' WHERE ' . $where; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); return $stmt->rowCount(); } //第四个read public function read($fields='*', $where='', $limit='0, 5') { //设置条件 $where = empty($where) ? '' : ' WHERE ' . $where; // 设置显示数量 $limit = ' LIMIT ' . $limit; $sql_base_a = "select * from fubao_user where id='4' limit '0,5' "; $sql_base_b = "select id,username,nickname,status from fubao_user where title like 'z%' limit '0,5' "; $sql = 'SELECT ' . $fields . ' FROM ' . $this->table. $where . $limit; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(\PDO::FETCH_ASSOC); } } //先实例化 $db = new DB('fubao_user'); // 查询 $fields_read = 'id,username,nickname,status'; $result_read = $db->read('id,username,nickname,status','',5); // 新增 $data_create = [ 'username' => 'xiaomao', 'password' => '123456', 'nickname' => '金刀驸马', 'status' => '1' ]; $result_create = $db->create($data_create); // 更新 $data_update = [ 'password'=>'2daada2', 'status'=>2 ]; $where_update = 'id=40'; $result_update = $db->update($data_update,$where_update); // 删除 $where_del = 'id = 44'; $result_del = $db->del($where_del); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <style> /*给表格加上边框*/ table { border: 1px solid #444444; border-collapse: collapse; width: 800px; margin: 20px auto; } th, td { border: 1px solid #444444; text-align: center; padding: 10px; } table caption { font-size: 1.3rem; /*文本加粗*/ font-weight: bolder; margin-bottom: 15px; } /* 第一行 */ #table-head-tr { background-color: lightgreen; } /* 第一行 */ table thead>tr:first-of-type { /* background-color: lightgreen; */ color: cornflowerblue; } table tbody>tr:first-of-type>td:first-of-type { background-color: wheat; } table tbody>tr:nth-last-of-type(1)>td:first-of-type { background-color: crimson; color: darkorange; } /*圆角表格样式*/ /* 第一行左 */ table tr:first-child th:first-child { border-top-left-radius: 12px; } /* 第一行右 */ table tr:first-child th:last-child { border-top-right-radius: 12px; } </style> <body> <table> <caption>数据库输出select</caption> <thead> <tr> <th>ID</th> <th>username</th> <th>nickname</th> <th>status</th> </tr> </thead> <tbody> <?php foreach( $result_read as $data){ ?> <tr> <td><?php echo $data['id']; ?></td> <td><?php echo $data['username']; ?></td> <td><?php echo $data['nickname']; ?></td> <td><?php echo $data['status']; ?></td> </tr> <?php } ?> </table> <hr> <h3> <?php echo '成功的新增了: '. $result_create['count'].' 条记录,新增的记录的主键ID是: ' . $result_create['id']; ?> </h3> <hr> <h3> <?php echo '成功的更新了: ' . $result_update. ' 条记录'; ?> </h3> <hr> <h3> <?php echo '成功的删除了: ' . $result_del . ' 条记录'; ?> </h3> </body> </html>
点击 "运行实例" 按钮查看在线实例