Blogger Information
Blog 65
fans 2
comment 0
visits 60668
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO遇上OOP燃爆(CURD),瞬间理解透彻'抽象',小白摇身变大神!
张福根一修品牌运营
Original
683 people have browsed it

抽象理解透彻:update , delete, insert, Select操作

案例效果:

OOP操作数据库


数据表

案例源码:

一、定义接口:

  1. interface jkDbBase
  2. {
  3. //数据库插入操作
  4. static function insert($db,$data);
  5. //数据库查询操作
  6. static function select($db,$options=[]);
  7. //数据库删除操作
  8. static function delete($db,$where);
  9. // 数据库更新操作
  10. static function update($db,$options=[]);
  11. }

二、定义中间类/抽象类:

  1. abstract class jkDb implements JKDbBase {
  2. protected static $db = null;
  3. public static function connect($dsn,$username,$password)
  4. {
  5. // 判断是否连接数据库
  6. if(is_null(self::$db)){
  7. self::$db = new PDO($dsn,$username,$password);
  8. }
  9. return self::$db;
  10. }
  11. }

三、工作类,数据库CURD操作

  1. class DB extends jkDb
  2. {
  3. // 数据插入
  4. static function insert($db,$data)
  5. {
  6. $sql = 'INSERT `user` SET `uname`=?,`pwd`=?,`age`=?;';
  7. $stmt = $db->prepare($sql);
  8. $stmt -> execute($data);
  9. return $stmt;
  10. }
  11. // 数据查询
  12. static function select($db,$options=[]){
  13. return $db->query('SELECT * FROM `user` LIMIT 2')->fetchAll(PDO::FETCH_ASSOC);
  14. }
  15. // 数据删除
  16. static function delete($db,$where){
  17. $sql3 = 'DELETE FROM `user` WHERE `id`= ?;';
  18. $stmt3 = $db->prepare($sql3);
  19. $stmt3 -> execute([8]);
  20. return $stmt3;
  21. }
  22. // 数据更新
  23. static function update($db,$data4=[])
  24. {
  25. $sql4 = 'UPDATE `user` SET `uname` = ?,`pwd`=?,`age`=? WHERE `id`=?;';
  26. $stmt4 = $db->prepare($sql4);
  27. $stmt4 ->execute($data4);
  28. return $stmt4;
  29. }
  30. }

四、数据库配置

  1. $config = [
  2. 'type' => $type ?? 'mysql',
  3. 'host' => $host ?? 'localhost',
  4. 'dbname' => $dbname??'study',
  5. 'username' => $username ?? 'root',
  6. 'password' => $password ?? '123456',
  7. ];

五、PDO数据连接数据库

  1. $dsn = sprintf('%s:host=%s;dbname=%s;',$config['type'],$config['host'],$config['dbname']);
  2. $username = $config['username'];
  3. $password = $config['password'];
  4. $db = DB::connect($dsn,$username,$password);
  5. // 设置错误级别
  6. $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);

六、数据输出

  1. // 1、插入数据
  2. $data = ['二狗',md5('123456'),26];
  3. $stmt = DB::insert($db,$data);
  4. if($stmt->rowCount() > 0) echo '插入成功' . $stmt->rowCount() . '条记录,新增数据的id为:' . $db->lastInsertId();
  5. // 输出:插入成功1条记录,新增数据的id为:7
  6. echo "<hr>";
  7. // 2、查询数据
  8. foreach ((DB::select($db))as $user)
  9. {
  10. print_r($user);
  11. }
  12. // 输出:Array ( [id] => 1 [uname] => admin [pwd] => e10adc3949ba59abbe56e057f20f883e [age] => 20 ) Array ( [id] => 2 [uname] => fugen [pwd] => e10adc3949ba59abbe56e057f20f883e [age] => 30 )
  13. echo "<hr>";
  14. // 3、删除数据
  15. $stmt3 = DB::delete($db,$stmt3);
  16. if($stmt3->rowCount() > 0) echo '删除成功' . $stmt3->rowCount() . '条记录';
  17. // 输出:删除成功1条记录
  18. echo "<hr>";
  19. // 4、更新数据
  20. $data4 = ['毛毛',md5('123456'),52,6];
  21. $stmt4 = DB::update($db,$data4);
  22. if($stmt4->rowCount() > 0) echo '更新成功' . $stmt4->rowCount() . '条记录';
  23. // 输出:更新成功1条记录

案例总结:

OOP知识:

  • interface,接口是特殊的抽象类,接口是定义,类是实现;
  • 抽象类:设计与实现分离,抽象类为子类定义公共接口,具体实现交由子类完成;

PDO知识:

  • PDO数据连接~三要素 DSN数据源 username password;
  • prepare() — 准备要执行的语句,并返回语句对象 object(PDOStatement);
  • PDOStatement::execute — 执行一条预处理语句;
  • sql查询 “SELECT * FROM user WHERE id=1”;
  • sql删除 DELETE FROM TABLE_NAME WHERE CLAUSE;
  • sql插入 insert into table_name (column_name)values();
  • sql更新 UPDATE table_name SET column_name=new_value,…[WHERE Clause];
Correcting teacher:灭绝师太灭绝师太

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
灭绝师太 2020-12-07 17:42:57
函数体中使用到的变量要从参数列表中传进来,并且要将函数体执行的结果return给调用者,调用者(外部)才能使用函数的执行结果,相信这两点你现在应该理解比较透彻了~
1 floor
Author's latest blog post