Blogger Information
Blog 52
fans 1
comment 1
visits 38823
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Db类中数据库增删改查方法
小丑0o鱼
Original
620 people have browsed it
  1. <?php
  2. /**
  3. * 数据库工具类,采用pdo方式
  4. * 当前仅支持MySql
  5. */
  6. class Db{
  7. public function __construct(){
  8. // 数据库连接池
  9. $this->pdo_list = [];
  10. }
  11. // 初始化pdo
  12. private function init($db){
  13. if(isset($this->pdo_list[$db]) && $this->pdo_list[$db]){
  14. $this->pdo = $this->pdo_list[$db];
  15. return;
  16. }
  17. // 数据库配置
  18. $dsn = "mysql:host=127.0.0.1;dbname=imqq";
  19. $this->pdo = new \PDO($dsn,'root','root');
  20. $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  21. $this->pdo_list[$db] = $this->pdo;
  22. }
  23. // 指定表名,支持多数据库
  24. public function table($table,$db='default'){
  25. $this->init($db);
  26. $this->table = $table;
  27. $this->field = '*';
  28. $this->order = '';
  29. $this->limit = 0;
  30. $this->where = [];
  31. $this->lastsql = '';
  32. $this->binds = [];
  33. return $this;
  34. }
  35. // 指定查询字段
  36. public function field($field='*'){
  37. $this->field = $field;
  38. return $this;
  39. }
  40. // 指定where条件
  41. public function where($where){
  42. $this->where = $where;
  43. return $this;
  44. }
  45. // 限制结果数量
  46. public function limit($limit){
  47. $this->limit = $limit;
  48. return $this;
  49. }
  50. // 排序
  51. public function order($order=''){
  52. $this->order = $order;
  53. return $this;
  54. }
  55. // 查询一条记录
  56. public function item(){
  57. $sql = $this->build_sql('select').' limit 1';
  58. $stmt = $this->pdo->prepare($sql);
  59. $this->bindValue($stmt);
  60. $stmt->execute();
  61. $item = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  62. return $item ? $item[0] : false;
  63. }
  64. // 查询列表
  65. public function lists(){
  66. $sql = $this->build_sql('select');
  67. $stmt = $this->pdo->prepare($sql);
  68. $this->bindValue($stmt);
  69. $stmt->execute();
  70. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  71. }
  72. // 自定义索引列表
  73. public function cates($index){
  74. $result = [];
  75. $lists = $this->lists();
  76. if(!$lists){
  77. return $result;
  78. }
  79. foreach ($lists as $key => $value) {
  80. $result[$value[$index]] = $value;
  81. }
  82. return $result;
  83. }
  84. // 查询总数
  85. public function count(){
  86. $sql = $this->build_sql('count');
  87. $stmt = $this->pdo->prepare($sql);
  88. $this->bindValue($stmt);
  89. $stmt->execute();
  90. $total = $stmt->fetchColumn(0);
  91. return $total;
  92. }
  93. // 分页
  94. public function pages($page = 1,$pageSize = 10,$path=''){
  95. $this->limit = ($page-1)*$pageSize.','.$pageSize;
  96. $total = $this->count();
  97. $data = $this->lists();
  98. $pages = $this->_subPages($page,$pageSize,$total,$path);
  99. $result = array('total'=>$total,'data'=>$data,'page'=>$page,'pages'=>$pages);
  100. return $result;
  101. }
  102. // 添加记录
  103. public function insert($data){
  104. $sql = $this->build_sql('insert',$data);
  105. $this->sqls[] = $sql;
  106. $stmt = $this->pdo->prepare($sql);
  107. $this->bindValue($stmt,$data);
  108. $stmt->execute();
  109. return $this->pdo->lastInsertId();
  110. }
  111. // 修改记录
  112. public function update($data){
  113. $sql = $this->build_sql('update',$data);
  114. $stmt = $this->pdo->prepare($sql);
  115. $this->bindValue($stmt,$data);
  116. return $stmt->execute();
  117. }
  118. // 删除记录
  119. public function delete(){
  120. $sql = $this->build_sql('delete');
  121. $stmt = $this->pdo->prepare($sql);
  122. $this->bindValue($stmt);
  123. return $stmt->execute();
  124. }
  125. // 构造sql
  126. private function build_sql($type,$data = null){
  127. $sql = '';
  128. // query
  129. if($type == 'select'){
  130. $where = $this->_build_where();
  131. $sql = "SELECT {$this->field} FROM {$this->table}" . ($where ? " WHERE {$where}" : '');
  132. $this->order && $sql .= " order by {$this->order}";
  133. $this->limit && $sql .= " limit {$this->limit}";
  134. }
  135. // count
  136. if($type == 'count'){
  137. $where = $this->_build_where();
  138. $field = count(explode(',',$this->field))>1?'*':$this->field;
  139. $sql = "SELECT count({$field}) FROM {$this->table}" . ($where ? " WHERE {$where}" : '');
  140. }
  141. // insert
  142. if($type == 'insert'){
  143. $sql = $this->_build_insert($data);
  144. }
  145. // update
  146. if($type == 'update'){
  147. $sql = $this->_build_update($data);
  148. }
  149. // delete
  150. if($type == 'delete'){
  151. $sql = $this->_build_delete();
  152. }
  153. $this->lastsql = $sql;
  154. return $sql;
  155. }
  156. // 构造查询条件where
  157. private function _build_where(){
  158. $where = '';
  159. if(is_array($this->where)){
  160. foreach ($this->where as $key => $item) {
  161. $where .= " and `{$key}`=:{$key}";
  162. }
  163. }else{
  164. $where = $this->where;
  165. }
  166. $where = ltrim($where,' and');
  167. return $where;
  168. }
  169. // 构造添加数据sql
  170. private function _build_insert($data){
  171. if(!$data){
  172. return false;
  173. }
  174. $sql = "insert into {$this->table}";
  175. $fields = $values = [];
  176. foreach ($data as $key => $val) {
  177. $fields[] = '`'.$key.'`';
  178. $values[] = ":$key";
  179. }
  180. $sql .= ('('.implode(',',$fields).')values('.implode(',',$values).')');
  181. return $sql;
  182. }
  183. // 构造更新数据sql
  184. private function _build_update($data){
  185. $where = $this->_build_where();
  186. $str_update = '';
  187. foreach ($data as $key => $val) {
  188. $str_update .= ('`'.$key.'`=:'.$key.',');
  189. }
  190. $sql = "UPDATE {$this->table} SET {$str_update}";
  191. $sql = rtrim($sql,',');
  192. $sql .= ' WHERE '.$where;
  193. return $sql;
  194. }
  195. // 构造删除数据sql
  196. private function _build_delete(){
  197. $where = $this->_build_where();
  198. $sql = "DELETE FROM {$this->table}" . ($where ? " WHERE {$where}" : '');
  199. return $sql;
  200. }
  201. // 参数绑定
  202. private function bindValue($stmt,$data = null){
  203. if($this->where && is_array($this->where)){
  204. foreach($this->where as $key => $item){
  205. $stmt->bindValue(':'.$key,$item);
  206. $this->binds[$key] = $item;
  207. }
  208. }
  209. if($data){
  210. foreach ($data as $k => $value) {
  211. $stmt->bindValue(':'.$k,$value);
  212. $this->binds[$k] = $value;
  213. }
  214. }
  215. }
  216. // 获取最后执行的sql
  217. public function getlastsql(){
  218. if(!$this->lastsql){
  219. return '';
  220. }
  221. foreach ($this->binds as $key => $value) {
  222. $value = is_string($value) ? "'".$value."'" : $value;
  223. $this->lastsql = str_replace(':'.$key, $value, $this->lastsql);
  224. }
  225. echo $this->lastsql.'<br>';
  226. }
  227. // 构造分页(bootstrap风格)
  228. // cur_page:当前第几页
  229. private function _subPages($cur_page,$pageSize,$total=0,$path=''){
  230. $html = '';
  231. // 分页数
  232. $page_count = ceil($total / $pageSize);
  233. if($page_count == 1){
  234. return $html;
  235. }
  236. // path
  237. $symbol = '?';
  238. $is_and = strpos($path,'?');
  239. if($is_and !== false && $is_and >= 0){
  240. $symbol = '&';
  241. }
  242. // 添加“首页”
  243. if($cur_page>1){
  244. $pre_page = $cur_page-1;
  245. $html = "<li><a href='{$path}{$symbol}page=1'><span>首页</span></a></li>";
  246. $html .= "<li><a href='{$path}{$symbol}page={$pre_page}'><span>下一页</span></a></li>";
  247. }
  248. // 每次最多显示几页
  249. $max_page_limit = 6;
  250. // 当前页向前显示几页
  251. $cur_page_pre = (int)$max_page_limit/2;
  252. // 第一页
  253. $start = $cur_page > ($page_count - $max_page_limit) ? ($page_count - $max_page_limit) : $cur_page;
  254. // 最后一页
  255. $end = ($cur_page + $max_page_limit) >$page_count ? $page_count : ($cur_page + $max_page_limit);
  256. if($start - $cur_page_pre > 0){
  257. $start = $start - $cur_page_pre;
  258. $end = $end -$cur_page_pre;
  259. }
  260. if($cur_page+$cur_page_pre>=$end && $page_count>$max_page_limit){
  261. $start = $start + $cur_page_pre;
  262. $end = $end +$cur_page_pre;
  263. }
  264. $start = $start <= 0 ? 1 : $start;
  265. for($i=$start;$i<$end;$i++){
  266. $html .= $cur_page == $i ? "<li class='active'><a>{$i}</a></li>":"<li><a href='{$path}{$symbol}page={$i}'>{$i}</a></li>";
  267. }
  268. // 添加尾页
  269. if($cur_page<$page_count){
  270. $after_page = $cur_page+1;
  271. $html .= "<li><a href='{$path}{$symbol}page={$after_page}'>下一页</a></li>";
  272. $html = $html . "<li><a href='{$path}{$symbol}page={$page_count}'>尾页</a></li>";
  273. }
  274. $html = '<ul class="pagination">'.$html.'</ul>';
  275. return $html;
  276. }
  277. }
Correction status:Uncorrected

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