Blogger Information
Blog 29
fans 0
comment 0
visits 14128
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
查询构造器实践
P粉317509817
Original
417 people have browsed it

查询构造器

代码

  1. <?php
  2. namespace ns;
  3. use PDO;
  4. class DB
  5. {
  6. protected $db;//数据库对象
  7. protected $table;//表名
  8. protected $field;//字段名
  9. protected $limit;//限制
  10. protected $opt =[];//具体操作
  11. // 链接数据库
  12. public function __construct($dsn,$username,$password)
  13. {
  14. $this->db= new PDO($dsn,$username,$password);
  15. }
  16. // 表名赋值
  17. public function table($table)
  18. {
  19. $this->table = $table;
  20. return $this;
  21. }
  22. // 字段赋值
  23. public function field($field)
  24. {
  25. $this->field = $field;
  26. return $this;
  27. }
  28. //分页操作
  29. public function page($page =1)
  30. {
  31. $this->opt['offset']=' OFFSET '.($page -1 )*$this->limit;
  32. return $this;
  33. }
  34. //限制设置
  35. public function limit($limit = 10)
  36. {
  37. $this->limit = $limit;
  38. $this->opt['limit'] = "LIMIT $limit";
  39. return $this;
  40. }
  41. //查询条件
  42. public function where($where = '')
  43. {
  44. $this->opt['where']= "WHERE $where";
  45. return $this;
  46. }
  47. // 查询
  48. public function select()
  49. {
  50. $sql = 'SELECT '. $this->field . ' FROM '.$this->table;
  51. $sql.=$this->opt['where'] ?? null;
  52. $sql.=$this->opt['limit'] ?? null;
  53. $sql.=$this->opt['offset'] ?? null;
  54. $stmt = $this->db->prepare($sql);
  55. $stmt->execute();
  56. $this->opt['where'] = null;
  57. return $stmt->fetchAll();
  58. }
  59. // 插入操作
  60. public function insert($data)
  61. {
  62. $str='';
  63. foreach ($data as $key=>$value)
  64. {
  65. $str.=$key.'= "'.$value.'",';
  66. }
  67. $sql = 'INSERT '.$this->table . 'SET '.rtrim($str,',');
  68. $stmt=$this->db->prepare($sql);
  69. $stmt->execute();
  70. $this->opt['where'] = null;
  71. return $stmt->rowCount();
  72. }
  73. //更新操作
  74. public function updata($data)
  75. {
  76. $str='';
  77. foreach($data as $key=>$value){
  78. $str.=$key.'= "'.$value.'",';
  79. }
  80. $sql= 'UPDATE '.$this->table.'SET'.rtrim($str,',');
  81. $stmt=$this->db->prepare($sql);
  82. $stmt->execute();
  83. $this->opt['where']=null;
  84. return $stmt->rowCount();
  85. }
  86. //删除操作
  87. public function delete()
  88. {
  89. $sql = 'DELETE FROM ' .$this->table;
  90. $sql.= $this->opt['where']??die('禁止无条件删除');
  91. $stmt= $this->db->prepare($sql);
  92. $stmt->execute();
  93. $this->opt['where']=null;
  94. return $stmt->rowCount();
  95. }
  96. }

实例化

1、查询

  1. $db = new DB('mysql:dbname=phpedu','root','root');
  2. $res = $db->table('people')->field('id,name,email')->select();
  3. printf('<pre>%s</pre>',print_r($res,true));
效果:

2、条件查询

  1. $res = $db->table('people')
  2. ->field('id,name,email')
  3. ->where('id > 20')
  4. ->limit(3)
  5. ->page(2)
  6. ->select();
  7. printf('<pre>%s</pre>',print_r($res,true));
效果:

3、删除

  1. $n = $db->table('people')->where('id=12')->delete();
  2. echo $n > 0 ? '删除成功<br>' : '删除失败或没有数据被删除<br>';
效果:

4、新增操作

  1. $n = $db->table('people')
  2. ->insert(['name' => 'Jack',
  3. 'email' => 'jack@php.cn',
  4. 'gender' => 1]);
  5. echo $n > 0 ? '<br>新增成功<br>' : '<br>新增失败或没有数据被添加<br>';
效果:


5、更新操作

  1. $n = $db->table('people')->updata(['name' => 'zhu']);
  2. echo $n > 0 ? '更新成功<br>' : '更新失败或没有数据被更新<br>';
效果:

6、删除操作

  1. $n = $db->table('people')->where('id = 7')->delete();
  2. echo $n > 0 ? '<br>删除成功<br>' : '<br>删除失败或没有数据被删除<br>';
效果:

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