Blogger Information
Blog 15
fans 0
comment 0
visits 7783
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
参考demo3.php, 完善数据库查询构造器其他的操作,update,delete,insert
ッ小眼睛っ
Original
560 people have browsed it
  1. <?php
  2. // 单例模式连接数据库
  3. interface iDbBase
  4. {
  5. //数据库操作 curd
  6. static function insert($db,$data);
  7. static function select($db,$where=[]);
  8. static function delete($db,$where=[]);
  9. static function update($db,$data,$where=[]);
  10. static function doConnect($dsn,$username,$password);
  11. }
  12. abstract class aDb implements iDbBase
  13. {
  14. // 创建类的唯一实例 pdo对象
  15. private static $_instance;
  16. // private私有的 阻止此类在外部进行实例化
  17. private function __construct()
  18. {
  19. }
  20. // private私有的 阻止此类在外部进行克隆
  21. private function __clone()
  22. {
  23. }
  24. static function doConnect($dsn,$username,$password)
  25. {
  26. // 得到pdo连接对象 储存在 $_instance
  27. if(is_null(self::$_instance))
  28. {
  29. self::$_instance = new PDO($dsn,$username,$password);
  30. }
  31. return self::$_instance;
  32. }
  33. }
  34. // 工作类
  35. class Db extends aDb{
  36. //数据库操作 curd
  37. static function insert($db,$data=''){
  38. return $db->query("INSERT INTO t_user ( name, type)VALUES('张杰','1')")->fetchAll(PDO::FETCH_ASSOC);
  39. }
  40. static function select($db,$where=['type'=>1]){
  41. // select filed.. form tableName where gender=1 and id>1
  42. // select filed.. form tableName where gender=1
  43. $str = '';
  44. foreach($where as $k=>$v)
  45. {
  46. if(is_array($where))
  47. {
  48. if(count($where) > 1)
  49. {
  50. $str .= $k . ' = ' . $v . ' and ';
  51. }else{
  52. $str .= $k . '=' . $v;
  53. }
  54. }
  55. }
  56. if(count($where) > 1)
  57. {
  58. // 去掉where中的最后一个and
  59. $str = substr($str,0,strlen($str)-4);
  60. // echo $str;
  61. }
  62. return $db->query("SELECT * FROM `t_user` WHERE $str ")->fetchAll(PDO::FETCH_ASSOC);
  63. }
  64. static function delete($db,$where=['id'=>4]){
  65. $str = '';
  66. foreach($where as $k=>$v)
  67. {
  68. if(is_array($where))
  69. {
  70. if(count($where) > 1)
  71. {
  72. $str .= $k . ' = ' . $v . ' and ';
  73. }else{
  74. $str .= $k . '=' . $v;
  75. }
  76. }
  77. }
  78. if(count($where) > 1)
  79. {
  80. // 去掉where中的最后一个and
  81. $str = substr($str,0,strlen($str)-4);
  82. // echo $str;
  83. }
  84. return $db->query("DELETE FROM t_user WHERE $str")->fetchAll(PDO::FETCH_ASSOC);
  85. }
  86. static function update($db,$data=2,$where=['name'=>'周杰伦','type'=>2]){
  87. $str = '';
  88. foreach($where as $k=>$v)
  89. {
  90. if(is_array($where))
  91. {
  92. if(count($where) > 1)
  93. {
  94. $str .= $k . ' = ' . '\'' . $v . '\''. ' , ';
  95. }else{
  96. $str .= $k . '=' . $v;
  97. }
  98. }
  99. }
  100. if(count($where) > 1)
  101. {
  102. // 去掉where中的最后一个and
  103. $str = substr($str,0,strlen($str)-2);
  104. echo $str;
  105. }
  106. return $db->query("UPDATE t_user SET $str WHERE id = '$data'")->fetchAll(PDO::FETCH_ASSOC);
  107. }
  108. }
  109. // 客户端代码
  110. $dsn = 'mysql:host=localhost;dbname=test_db';
  111. $db = Db::doConnect($dsn,'test_db','123456');
  112. //var_dump($db);
  113. //print_r(Db::select($db));
  114. //print_r(Db::insert($db));
  115. //print_r(Db::delete($db));
  116. 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