Blogger Information
Blog 43
fans 0
comment 0
visits 30362
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单例模式(数据库crud操作)
橙絮圆
Original
492 people have browsed it

单例模式(数据库crud操作)

作业标题:0813 oop编程-3
作业内容:参考demo3.php, 完善数据库查询构造器其他的操作,update,delete,insert


  • insert,delete,update
  1. interface iDbBase
  2. {
  3. //数据库操作 curd
  4. static function insert($db,$data);
  5. static function select($db,$where=[]);
  6. static function delete($db,$where=[]);
  7. static function update($db,$data,$where=[]);
  8. static function doConnect($dsn,$username,$password);
  9. }
  10. abstract class aDb implements iDbBase
  11. {
  12. // 创建类的唯一实例 pdo对象
  13. private static $_instance;
  14. // private私有的 阻止此类在外部进行实例化
  15. private function __construct()
  16. {
  17. }
  18. // private私有的 阻止此类在外部进行克隆
  19. private function __clone()
  20. {
  21. }
  22. static function doConnect($dsn,$username,$password)
  23. {
  24. // 得到pdo连接对象 储存在 $_instance
  25. if(is_null(self::$_instance))
  26. {
  27. self::$_instance = new PDO($dsn,$username,$password);
  28. }
  29. return self::$_instance;
  30. }
  31. }
  32. // 工作类
  33. class Db extends aDb{
  34. //数据库操作 curd
  35. static function insert($db,$data){
  36. return $db->query($data);
  37. }
  38. static function select($db,$where=['empno'=>7369]){
  39. // select filed.. form tableName where gender=1 and id>1
  40. // select filed.. form tableName where gender=1
  41. $str = '';
  42. foreach($where as $k=>$v)
  43. {
  44. if(is_array($where))
  45. {
  46. if(count($where) > 1)
  47. {
  48. $str .= $k . ' = ' . $v . ' and ';
  49. }else{
  50. $str .= $k . '=' . $v;
  51. }
  52. }
  53. }
  54. if(count($where) > 1)
  55. {
  56. // 去掉where中的最后一个and
  57. $str = substr($str,0,strlen($str)-4);
  58. // echo $str;
  59. }
  60. echo $str;
  61. // SELECT `uname`, `tel` FROM `iuser` WHERE gender = 1 and id = 20201
  62. return $db->query("SELECT `ename`, `job` FROM `emp` WHERE $str ")->fetchAll(PDO::FETCH_ASSOC);
  63. }
  64. static function delete($db,$where=['empno'=>7936]){
  65. //DELETE FROM Persons WHERE LastName='Griffin'
  66. $str = '';
  67. foreach($where as $k=>$v)
  68. {
  69. if(is_array($where))
  70. {
  71. if(count($where) > 1)
  72. {
  73. $str .= $k . ' = ' . $v . ' and ';
  74. }else{
  75. $str .= $k . '=' . $v;
  76. }
  77. }
  78. }
  79. if(count($where) > 1)
  80. {
  81. // 去掉where中的最后一个and
  82. $str = substr($str,0,strlen($str)-4);
  83. // echo $str;
  84. }
  85. echo $str;
  86. // DELETE FROM Persons WHERE LastName='Griffin'
  87. return $db->query("DELETE FROM `emp` WHERE $str ")->fetchAll(PDO::FETCH_ASSOC);
  88. }
  89. static function update($db,$data=1000,$where=['empno'=>7938]){
  90. $str = '';
  91. foreach($where as $k=>$v)
  92. {
  93. if(is_array($where))
  94. {
  95. if(count($where) > 1)
  96. {
  97. $str .= $k . ' = ' . $v . ' and ';
  98. }else{
  99. $str .= $k . '=' . $v;
  100. }
  101. }
  102. }
  103. if(count($where) > 1)
  104. {
  105. // 去掉where中的最后一个and
  106. $str = substr($str,0,strlen($str)-4);
  107. // echo $str;
  108. }
  109. echo $str;
  110. // UPDATE Persons SET Age=36 WHERE FirstName='Peter' AND LastName='Griffin'
  111. return $db->query("UPDATE `emp` SET salary=$data WHERE $str ");
  112. }
  113. }
  114. $dsn = 'mysql:host=localhost;dbname=my_test';
  115. $db = Db::doConnect($dsn,'root','root');
  116. $data="INSERT INTO emp "."(empno,ename,job,mgr,hiredate,salary,comm,deptno)"."VALUES"."('7938','张同','职员','7900','2012-08-07',3000,400,30)";
  117. print_r(Db::select($db));
  118. print_r(Db::insert($db,$data));
  119. print_r(Db::delete($db));
  120. print_r(Db::update($db));
Correcting teacher:PHPzPHPz

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