Blogger Information
Blog 38
fans 0
comment 0
visits 18521
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
查询构造器基类
Blackeye
Original
443 people have browsed it

123

  1. <?php
  2. namespace phpedu;
  3. use PDO;
  4. class DB{
  5. protected $db;
  6. protected $table;
  7. protected $field;
  8. protected $limit;
  9. protected $opt = [];
  10. public function __construct($dsn,$username,$password){
  11. $this->db = new PDO($dsn,$username,$password);
  12. }
  13. // 获取数据表
  14. public function table($table){
  15. $this->table = $table;
  16. return $this;
  17. }
  18. // 获取查询字段
  19. public function field($field){
  20. $this->field = $field;
  21. return $this;
  22. }
  23. // 获取位置限制条件
  24. public function where($where=''){
  25. $this->opt["where"] = " where $where";
  26. return $this;
  27. }
  28. // 获取查询限制
  29. public function limit($limit=10){
  30. $this->limit = $limit;
  31. $this->opt["limit"] = " LIMIT ".$this->limit;
  32. return $this;
  33. }
  34. // 获取偏移量
  35. public function page($page=1){
  36. $this->opt["offset"] = " OFFSET ".($page-1)*$this->limit;
  37. return $this;
  38. }
  39. // 清理查询条件助手
  40. private function cleaner(){
  41. foreach($this->opt as $key=>$item){
  42. $this->opt[$key]=null;
  43. }
  44. }
  45. private function execute($sql){
  46. $stmt = $this->db->prepare($sql);
  47. $stmt->execute();
  48. $this->cleaner();
  49. return $stmt;
  50. }
  51. // 查
  52. public function select(){
  53. $sql = 'SELECT ' . $this->field . ' FROM ' . $this->table;
  54. $sql .= $this->opt["where"] ?? null;
  55. $sql .= $this->opt["limit"] ?? null;
  56. $sql .= $this->opt["offset"] ?? null;
  57. return $this->execute($sql)->fetchAll();
  58. }
  59. // 增
  60. public function insert($data){
  61. $str = '';
  62. foreach ($data as $key=>$value){
  63. $str .= $key.'="'. $value .'", ';
  64. }
  65. $sql = 'INSERT '.$this->table . ' SET ' . rtrim($str, ', ');
  66. return $this->execute($sql)->rowCount();
  67. }
  68. // 改
  69. public function update($data){
  70. $str = '';
  71. foreach ($data as $key=>$value){
  72. $str .= $key.'="'. $value .'", ';
  73. }
  74. $sql = 'UPDATE '.$this->table . ' SET ' . rtrim($str, ', ');
  75. $sql .= $this->opt['where'] ?? die('禁止无条件更新');
  76. return $this->execute($sql)->rowCount();
  77. }
  78. // 删
  79. public function delete(){
  80. $sql = 'DELETE FROM ' . $this->table;
  81. $sql .= $this->opt['where'] ?? die('禁止无条件删除');
  82. return $this->execute($sql)->rowCount();
  83. }
  84. }
  85. $db = new Db('mysql:dbname=phpedu','root','root');
  86. $db_select = $db->table('staff')->field('id,name,gender')->limit(2)->select();
  87. printf("<pre>%s</pre>",print_r($db_select,true));
  88. $db->insert(['name'=>'David', 'gender'=>0, 'email'=>'David@qq.com']);
  89. $db->where('id = 1')->update(['name'=>'David', 'gender'=>0]);
  90. $db_select = $db->table('staff')->field('id,name,gender')->limit(1)->select();
  91. printf("<pre>%s</pre>",print_r($db_select,true));
  92. $db->where('id = 1')->delete();
  93. $db_select = $db->table('staff')->field('id,name,gender')->limit(1)->select();
  94. printf("<pre>%s</pre>",print_r($db_select,true));
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!