博客列表 >使用接口操作数据库

使用接口操作数据库

leverWang
leverWang原创
2020年07月24日 16:41:46908浏览

抽象类和接口:

  • 抽象类:不能直接实例化,可以在类中定义方法和属性。类似于模版,子类继承后再实现详细功能.
  • 接口: 接口中定义的所有方法都必须是公有,接口本身是抽象的,内部申明的方法也是抽象的; 不用加abstract,接口不能有属性、普通方法、可以有常量.

使用接口来操作数据库

  1. <?php
  2. $config = require 'config.php';
  3. //定义接口类
  4. interface iDB
  5. {
  6. // 增加
  7. public static function insert($pdo, $data);
  8. // 删除
  9. public static function delete($pdo, $where);
  10. // 查询
  11. public static function select($pdo, $options = []);
  12. // 修改
  13. public static function update($pdo, $options);
  14. }
  15. //定义抽象类实现接口,封装公共数据库连接方法
  16. abstract class aDB implements iDB
  17. {
  18. protected static $pdo;
  19. //数据库连接方法
  20. public static function conn($dsn, $user, $pwd)
  21. {
  22. //判断数据库连接对象是否存在
  23. if (is_null(self::$pdo)) {
  24. try {
  25. $pdo = new PDO($dsn, $user, $pwd);
  26. // 设置结果集的默认获取模式
  27. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  28. return $pdo;
  29. } catch (PDOException $e) {
  30. exit('Connection Error: ' . $e->getMessage());
  31. }
  32. }
  33. return self::$pdo;
  34. }
  35. }
  36. //实现类,实现接口中的方法
  37. class DB extends aDB
  38. {
  39. // 增加
  40. public static function insert($pdo, $data)
  41. {
  42. $sql = "INSERT INTO `users`(`name`,`pwd`,`age`,`tel`) VALUES (?,?,?,?)";
  43. $stmt = $pdo->prepare($sql);
  44. $stmt->bindParam(1, $name, PDO::PARAM_STR, 50);
  45. $stmt->bindParam(2, $pwd, PDO::PARAM_STR, 50);
  46. $stmt->bindParam(3, $age, PDO::PARAM_INT, 30);
  47. $stmt->bindParam(4, $tel, PDO::PARAM_STR, 50);
  48. $num = 0;
  49. foreach ($data as $user) {
  50. extract($user);
  51. $stmt->execute() or die(print_r($stmt->errorInfo(), true));
  52. if ($stmt->rowCount() > 0) {
  53. $num += $stmt->rowCount();
  54. }
  55. }
  56. printf("成功添加 %s 条数据", $num);
  57. }
  58. // 删除
  59. public static function delete($pdo, $where)
  60. {
  61. $sql = "delete from `users` where `id` = ? ";
  62. $stmt = $pdo->prepare($sql);
  63. $stmt->bindParam(1, $where);
  64. $stmt->execute() or die(print_r('删除失败' . $stmt->errorInfo(), true));
  65. printf("成功删除%s条数据", $stmt->rowCount());
  66. }
  67. // 查询
  68. public static function select($pdo, $options = [])
  69. {
  70. $sql = "select `id`,`name`,`age` from `users` where id>= ?";
  71. $stmt = $pdo->prepare($sql);
  72. $stmt->bindParam(1,$options, PDO::PARAM_INT, 30);
  73. $stmt->execute() or die(print_r('查询失败' . $stmt->errorInfo()));
  74. foreach ($stmt->fetchAll() as $user) {
  75. vprintf("id:%s,name:%s,age:%s<br>", $user);
  76. }
  77. }
  78. // 修改
  79. public static function update($pdo, $options)
  80. {
  81. $sql = "update `users` set `name`=? where id=? ";
  82. $stmt = $pdo->prepare($sql);
  83. $stmt->bindParam(1, $options['name'], PDO::PARAM_STR, 50);
  84. $stmt->bindParam(2, $options['id'], PDO::PARAM_INT, 20);
  85. $stmt->execute() or die(print_r('更新数据失败:' . $stmt->errorInfo()));
  86. if ($stmt->rowCount() > 0):
  87. print_r('成功更新' . $stmt->rowCount() . '条数据');
  88. endif;
  89. }
  90. }
  91. extract($config);
  92. $dsn = "$type:host=$host;dbname=$dbname;charset=$charset;port=$port";
  93. $pdo = DB::conn($dsn, $username, $password);
  94. $users = [
  95. ['name' => 'Adalia1', 'age' => 33, 'pwd' => sha1('1256'), 'tel' => '188938801'],
  96. ['name' => 'Brenda1', 'age' => 12, 'pwd' => sha1('12456'), 'tel' => '178922801'],
  97. ['name' => 'Alice1', 'age' => 55, 'pwd' => sha1('12346'), 'tel' => '168448801'],
  98. ['name' => '骑牛狂奔1', 'age' => 65, 'pwd' => sha1('13456'), 'tel' => '138966801'],
  99. ['name' => 'Catherine1', 'age' => 111, 'pwd' => sha1('23456'), 'tel' => '118988801'],
  100. ];
  101. //增加
  102. //DB::insert($pdo, $users);
  103. //删除
  104. //DB::delete($pdo,110);
  105. //查询
  106. //DB::select($pdo,88);
  107. //更新
  108. DB::update($pdo,['name'=>'赵四333','id'=>118]);

总结:初步了解了接口和抽象类的定义和使用方法,但是对接口和抽象类在开发中的使用时机还不是很理解。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议