批改状态:合格
老师批语:如果你是第一次接触面向接口编程, 这种想法正常的, 能把这个案例写对很不容易
<?php$config = require 'config.php';//定义接口类interface iDB{// 增加public static function insert($pdo, $data);// 删除public static function delete($pdo, $where);// 查询public static function select($pdo, $options = []);// 修改public static function update($pdo, $options);}//定义抽象类实现接口,封装公共数据库连接方法abstract class aDB implements iDB{protected static $pdo;//数据库连接方法public static function conn($dsn, $user, $pwd){//判断数据库连接对象是否存在if (is_null(self::$pdo)) {try {$pdo = new PDO($dsn, $user, $pwd);// 设置结果集的默认获取模式$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);return $pdo;} catch (PDOException $e) {exit('Connection Error: ' . $e->getMessage());}}return self::$pdo;}}//实现类,实现接口中的方法class DB extends aDB{// 增加public static function insert($pdo, $data){$sql = "INSERT INTO `users`(`name`,`pwd`,`age`,`tel`) VALUES (?,?,?,?)";$stmt = $pdo->prepare($sql);$stmt->bindParam(1, $name, PDO::PARAM_STR, 50);$stmt->bindParam(2, $pwd, PDO::PARAM_STR, 50);$stmt->bindParam(3, $age, PDO::PARAM_INT, 30);$stmt->bindParam(4, $tel, PDO::PARAM_STR, 50);$num = 0;foreach ($data as $user) {extract($user);$stmt->execute() or die(print_r($stmt->errorInfo(), true));if ($stmt->rowCount() > 0) {$num += $stmt->rowCount();}}printf("成功添加 %s 条数据", $num);}// 删除public static function delete($pdo, $where){$sql = "delete from `users` where `id` = ? ";$stmt = $pdo->prepare($sql);$stmt->bindParam(1, $where);$stmt->execute() or die(print_r('删除失败' . $stmt->errorInfo(), true));printf("成功删除%s条数据", $stmt->rowCount());}// 查询public static function select($pdo, $options = []){$sql = "select `id`,`name`,`age` from `users` where id>= ?";$stmt = $pdo->prepare($sql);$stmt->bindParam(1,$options, PDO::PARAM_INT, 30);$stmt->execute() or die(print_r('查询失败' . $stmt->errorInfo()));foreach ($stmt->fetchAll() as $user) {vprintf("id:%s,name:%s,age:%s<br>", $user);}}// 修改public static function update($pdo, $options){$sql = "update `users` set `name`=? where id=? ";$stmt = $pdo->prepare($sql);$stmt->bindParam(1, $options['name'], PDO::PARAM_STR, 50);$stmt->bindParam(2, $options['id'], PDO::PARAM_INT, 20);$stmt->execute() or die(print_r('更新数据失败:' . $stmt->errorInfo()));if ($stmt->rowCount() > 0):print_r('成功更新' . $stmt->rowCount() . '条数据');endif;}}extract($config);$dsn = "$type:host=$host;dbname=$dbname;charset=$charset;port=$port";$pdo = DB::conn($dsn, $username, $password);$users = [['name' => 'Adalia1', 'age' => 33, 'pwd' => sha1('1256'), 'tel' => '188938801'],['name' => 'Brenda1', 'age' => 12, 'pwd' => sha1('12456'), 'tel' => '178922801'],['name' => 'Alice1', 'age' => 55, 'pwd' => sha1('12346'), 'tel' => '168448801'],['name' => '骑牛狂奔1', 'age' => 65, 'pwd' => sha1('13456'), 'tel' => '138966801'],['name' => 'Catherine1', 'age' => 111, 'pwd' => sha1('23456'), 'tel' => '118988801'],];//增加//DB::insert($pdo, $users);//删除//DB::delete($pdo,110);//查询//DB::select($pdo,88);//更新DB::update($pdo,['name'=>'赵四333','id'=>118]);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号