Blogger Information
Blog 56
fans 1
comment 0
visits 62699
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
对课堂demo4.php文件的insert,update,delect进行的编写。
零龙
Original
520 people have browsed it

对课堂数据库操作的编写

结合之前的课程,将demo4.php的文件的insert、update、delect方法进行了编写。

源码:

  1. <?php
  2. //接口应用场景:用抽象类来实现接口
  3. // 定义一个数据库的增删改查
  4. require __DIR__ ."/config.php";
  5. interface iDbase
  6. {
  7. public static function insert($db,$data);
  8. //添加
  9. public static function select($db,$option=[]);
  10. //查询
  11. public static function update($db,$option);
  12. //更新
  13. public static function delete($db,$where);
  14. //删除
  15. }
  16. // 实现类:使用抽象类来充当
  17. abstract class aDb implements iDbase
  18. {
  19. //使用单例模式连接:创建类的唯一对象
  20. //抽象类充当接口实现类时,不用必须实现接口中的抽象方法
  21. //当实现类中的方法中有一些公共操作时,可以将这些操作放在中间的抽象类中实现它
  22. protected static $db =null;
  23. public static function cennect($dsn,$username,$password)
  24. {
  25. if(is_null(self::$db)) //如果当前连接对象是:null,表示未连接数据库
  26. {
  27. self::$db = new PDO($dsn,$username,$password);
  28. }
  29. return self::$db;
  30. }
  31. }
  32. //类的真正实现类
  33. class DB extends aDb
  34. {
  35. public static function insert($db,$data)
  36. {
  37. extract($data);
  38. $sql='INSERT `user` SET `username`=?,`password`=?,`age`=?,`sex`=?,`email`=?,`mobile`=?;';
  39. $stmt = $db->prepare($sql);
  40. $stmt->execute([$username,$password,$sex,$age,$mobile,$email]);
  41. return '成功点击了'.$stmt->rowCount().'条数据';
  42. }
  43. //添加
  44. public static function select($db,$option=[])
  45. {
  46. return $db->query('SELECT * FROM `user` LIMIT 3')->fetchAll(PDO::FETCH_ASSOC);
  47. }
  48. //查询
  49. public static function update($db,$data)
  50. {
  51. extract($data);
  52. $id=334;
  53. $sql = 'UPDATE `user` SET `username`=?,`password`=?,`sex`=?,`age`=?,`email`=?,`mobile`=? WHERE `Id`>?;';
  54. $stmt = $db->prepare($sql);
  55. $stmt->execute([$username,$password,$sex,$age,$email,$mobile,$id]);
  56. return '更新了'.$stmt->rowCount().'条数据';
  57. }
  58. //更新
  59. public static function delete($db,$id)
  60. {
  61. $sql = 'DELETE FROM `user` WHERE `Id`>?;';
  62. $stmt = $db->prepare($sql);
  63. $stmt->execute([$id]);
  64. return '成功删除'.$stmt->rowCount().'条记录';
  65. }
  66. }
  67. $db = DB::cennect($dsn,$username,$password);
  68. // foreach (DB::select($db) as $user)
  69. // {
  70. // print_r($user);
  71. // }
  72. $data = [
  73. 'username'=>'嫦娥',
  74. 'password'=> sha1('8888'),
  75. 'age'=> 98,
  76. 'sex'=>'女',
  77. 'email'=> 'ce@163.com',
  78. 'mobile'=> '15687874598',
  79. ];
  80. $insert = new DB;
  81. $id = 334;
  82. echo $insert->insert($db,$data);
  83. echo $insert->update($db,$data,);
  84. echo $insert->delete($db,$id);

示例图:

总结:知道这是错误的写法,不过可以完成增删改查,在接下来的课程中在对此次作业进行正确的编写

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
0 comments
Author's latest blog post