博客列表 >查询构造器基类

查询构造器基类

Blackeye
Blackeye原创
2022年05月09日 09:20:53457浏览

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