DB.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. header("Content-Type:text/html;charset=utf-8");
  3. /**
  4. *php操作mysql的工具类
  5. */
  6. class Db{
  7. private $_db = null;//数据库连接句柄
  8. private $_table = null;//表名
  9. private $_where = null;//where条件
  10. private $_order = null;//order排序
  11. private $_limit = null;//limit限定查询
  12. private $_group = null;//group分组
  13. private $_configs = array(
  14. 'hostname' => 'localhost',
  15. 'dbname' => 'test',
  16. 'username' => 'root',
  17. 'password' => '1234'
  18. );//数据库配置
  19. /**
  20. * 构造函数,连接数据库
  21. */
  22. public function __construct($config = array()){
  23. $link = $this->_db;
  24. if (!empty($config)) $this->_configs = $config;
  25. if(!$link){
  26. $db = mysqli_connect($this->_configs['hostname'],$this->_configs['username'],$this->_configs['password'],$this->_configs['dbname']);
  27. mysqli_query($db,"set names utf8");
  28. if(!$db){
  29. $this->ShowException("错误信息".mysqli_connect_error());
  30. }
  31. $this->_db = $db;
  32. }
  33. }
  34. /**
  35. * 获取所有数据
  36. *
  37. * @param <type> $table The table
  38. *
  39. * @return boolean All.
  40. */
  41. public function getAll($table=null){
  42. $link = $this->_db;
  43. if(!$link)return false;
  44. $sql = "SELECT * FROM {$table}";
  45. $data = mysqli_fetch_all($this->execute($sql));
  46. return $data;
  47. }
  48. public function table($table){
  49. $this->_table = $table;
  50. return $this;
  51. }
  52. /**
  53. * 实现查询操作
  54. *
  55. * @param string $fields The fields
  56. *
  57. * @return boolean ( description_of_the_return_value )
  58. */
  59. public function select($fields="*"){
  60. $fieldsStr = '';
  61. $link = $this->_db;
  62. if(!$link)return false;
  63. if(is_array($fields)){
  64. $fieldsStr = implode(',', $fields);
  65. }elseif(is_string($fields)&&!empty($fields)){
  66. $fieldsStr = $fields;
  67. }
  68. $sql = "SELECT {$fields} FROM {$this->_table} {$this->_where} {$this->_order} {$this->_limit}";
  69. $res = $this->execute($sql);
  70. $list = array();
  71. while($row = mysqli_fetch_assoc($res)){
  72. $list[] = $row;
  73. }
  74. return $list;
  75. }
  76. /**
  77. * order排序
  78. *
  79. * @param string $order The order
  80. *
  81. * @return boolean ( description_of_the_return_value )
  82. */
  83. public function order($order=''){
  84. $orderStr = '';
  85. $link = $this->_db;
  86. if(!$link)return false;
  87. if(is_string($order)&&!empty($order)){
  88. $orderStr = "ORDER BY ".$order;
  89. }
  90. $this->_order = $orderStr;
  91. return $this;
  92. }
  93. /**
  94. * where条件
  95. *
  96. * @param string $where The where
  97. *
  98. * @return <type> ( description_of_the_return_value )
  99. */
  100. public function where($where=''){
  101. $whereStr = '';
  102. $link = $this->_db;
  103. if(!$link)return $link;
  104. if(is_array($where)){
  105. foreach ($where as $key => $value) {
  106. $whereArr[] = "`".$key."` = '".$value."'";
  107. }
  108. $whereStr = implode(' AND ', $whereArr);
  109. $whereStr = "WHERE ".$whereStr;
  110. }elseif(is_string($where)&&!empty($where)){
  111. $whereStr = "WHERE ".$where;
  112. }
  113. $this->_where = $whereStr;
  114. return $this;
  115. }
  116. /**
  117. * group分组
  118. *
  119. * @param string $group The group
  120. *
  121. * @return boolean ( description_of_the_return_value )
  122. */
  123. public function group($group=''){
  124. $groupStr = '';
  125. $link = $this->_db;
  126. if(!$link)return false;
  127. if(is_array($group)){
  128. $groupStr = "GROUP BY ".implode(',',$group);
  129. }elseif(is_string($group)&&!empty($group)){
  130. $groupStr = "GROUP BY ".$group;
  131. }
  132. $this->_group = $groupStr;
  133. return $this;
  134. }
  135. /**
  136. * limit限定查询
  137. *
  138. * @param string $limit The limit
  139. *
  140. * @return <type> ( description_of_the_return_value )
  141. */
  142. public function limit($limit=''){
  143. $limitStr = '';
  144. $link = $this->_db;
  145. if(!$link)return $link;
  146. if(is_string($limit)||!empty($limit)){
  147. $limitStr = "LIMIT ".$limit;
  148. }elseif(is_numeric($limit)){
  149. $limitStr = "LIMIT ".$limit;
  150. }
  151. $this->_limit = $limitStr;
  152. return $this;
  153. }
  154. /**
  155. * 执行sql语句
  156. *
  157. * @param <type> $sql The sql
  158. *
  159. * @return boolean ( description_of_the_return_value )
  160. */
  161. public function query($sql=null){
  162. $link = $this->_db;
  163. if(!$link)return false;
  164. $res = mysqli_query($this->_db,$sql);
  165. if(!$res){
  166. $errors = mysqli_error_list($this->_db);
  167. // $this->ShowException("报错啦!<br/>错误号:".$errors[0]['errno']."<br/>sql:".$sql."<br/>SQL错误状态:".$errors[0]['sqlstate']."<br/>错误信息:".$errors[0]['error']);
  168. die();
  169. }
  170. $list = array();
  171. while($row = mysqli_fetch_assoc($res)){
  172. $list[] = $row;
  173. }
  174. return $list;
  175. }
  176. /**
  177. * 执行sql语句
  178. *
  179. * @param <type> $sql The sql
  180. *
  181. * @return boolean ( description_of_the_return_value )
  182. */
  183. public function execute($sql=null){
  184. $link = $this->_db;
  185. if(!$link)return false;
  186. $res = mysqli_query($this->_db,$sql);
  187. if(!$res){
  188. $errors = mysqli_error_list($this->_db);
  189. // $this->ShowException("报错啦!<br/>错误号:".$errors[0]['errno']."<br/>sql:".$sql."<br/>SQL错误状态:".$errors[0]['sqlstate']."<br/>错误信息:".$errors[0]['error']);
  190. die();
  191. }
  192. $this->_table = null;//表名
  193. $this->_where = null;//where条件
  194. $this->_order = null;//order排序
  195. $this->_limit = null;//limit限定查询
  196. $this->_group = null;//group分组
  197. return $res;
  198. }
  199. /**
  200. * 插入数据
  201. *
  202. * @param <type> $data The data
  203. *
  204. * @return boolean ( description_of_the_return_value )
  205. */
  206. public function insert($data){
  207. $link = $this->_db;
  208. if(!$link)return false;
  209. if(is_array($data)){
  210. $keys = '';
  211. $values = '';
  212. foreach ($data as $key => $value) {
  213. $keys .= "`".$key."`,";
  214. $values .= "'".$value."',";
  215. }
  216. $keys = rtrim($keys,',');
  217. $values = rtrim($values,',');
  218. }
  219. $sql = "INSERT INTO `{$this->_table}`({$keys}) VALUES({$values})";
  220. mysqli_query($this->_db,$sql);
  221. $insertId = mysqli_insert_id($this->_db);
  222. return $insertId;
  223. }
  224. /**
  225. * 更新数据
  226. *
  227. * @param <type> $data The data
  228. *
  229. * @return <type> ( description_of_the_return_value )
  230. */
  231. public function update($data){
  232. $link = $this->_db;
  233. if(!$link)return $link;
  234. if(is_array($data)){
  235. $dataStr = '';
  236. foreach ($data as $key => $value) {
  237. $dataStr .= "`".$key."`='".$value."',";
  238. }
  239. $dataStr = rtrim($dataStr,',');
  240. }
  241. $sql = "UPDATE `{$this->_table}` SET {$dataStr} {$this->_where} {$this->_order} {$this->_limit}";
  242. $res = $this->execute($sql);
  243. return $res;
  244. }
  245. /**
  246. * 删除数据
  247. *
  248. * @return <type> ( description_of_the_return_value )
  249. */
  250. public function delete(){
  251. $link = $this->_db;
  252. if(!$link)return $link;
  253. $sql = "DELETE FROM `{$this->_table}` {$this->_where}";
  254. $res = $this->execute($sql);
  255. return $res;
  256. }
  257. /**
  258. * 异常信息输出
  259. *
  260. * @param <type> $var The variable
  261. */
  262. private function ShowException($var){
  263. if(is_bool($var)){
  264. var_dump($var);
  265. }else if(is_null($var)){
  266. var_dump(NULL);
  267. }else{
  268. echo "<pre style='position:relative;z-index:1000;padding:10px;border-radius:5px;background:#F5F5F5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;'>".print_r($var,true)."</pre>";
  269. }
  270. }
  271. // 断开mysql连接
  272. public function close(){
  273. mysqli_close($this->_db);
  274. }
  275. }