博客列表 >1127_类封装 第23课

1127_类封装 第23课

叮叮当当
叮叮当当原创
2019年11月30日 14:54:38867浏览

1. 把 php公用方法库中的 数据库操作函数, 重写到 Db类中。

  1. class Db{
  2. private $dsn;
  3. private $user;
  4. private $pwd;
  5. private $pdo;
  6. public function __construct($host, $dbname, $charset, $user, $pwd){
  7. $this->dsn = "mysql:host={$host};dbname={$dbname};charset={$charset}";
  8. $this->user = $user;
  9. $this->pwd = $pwd;
  10. $this->connect();
  11. }
  12. private function connect(){
  13. try {
  14. # 实例化PDO类,创建PDO对象
  15. $this->pdo = new PDO( $this->dsn, $this->user, $this->pwd );
  16. } catch (PDOException $e) {
  17. die('数据库错误:'.$e->getMessage());
  18. }
  19. }
  20. /**
  21. * 查询
  22. * @param $table
  23. * @param $fields 格式'id' 或 【'name','id']
  24. * @param $where 格式['id'=>['>',5],'name'=>'Jason']
  25. * @return array
  26. */
  27. public function select($table, $fields, $where=[], $order='', $limit=''){
  28. # 创建SQL语句
  29. $exe = [];
  30. $sql = 'SELECT ';
  31. if( is_array($fields) ){
  32. foreach( $fields as $field ){
  33. $sql .= $field.', ';
  34. }
  35. }
  36. else{
  37. $sql .= $fields;
  38. }
  39. $sql = rtrim( trim($sql),',' );
  40. $sql .= ' FROM '.$table;
  41. # 查询条件
  42. if( !empty($where) ){
  43. $sql .= ' WHERE ';
  44. foreach( $where as $k => $v ){
  45. if( is_array($v) ){
  46. $sql .= '`' .$k. '`' .$v[0]. ':' .$k. ' AND ';
  47. $exe[ $k ] = $v[1];
  48. }
  49. else{
  50. $sql .= '`' .$k. '`=:' .$k. ' AND ';
  51. $exe[ $k ] = $v;
  52. }
  53. }
  54. $sql = rtrim( trim($sql),'AND');
  55. }
  56. # 排序条件
  57. if( !empty($order) ){
  58. $sql .= ' order by '.$order;
  59. }
  60. # 分页条件
  61. if( !empty($limit) ){
  62. $sql .= ' limit '.$limit;
  63. }
  64. $sql .= ';';
  65. return $this->prepare_exe( 'select', $sql, $exe );
  66. }
  67. /**
  68. * 查询单条记录
  69. * @param $table
  70. * @param $fields
  71. * @param $where 格式['id'=>['>',5],'name'=>'Jason']
  72. * @return array
  73. */
  74. function find( $table, $fields, $where=[] ){
  75. # 创建SQL语句
  76. $exe = [];
  77. $sql = 'SELECT ';
  78. if( is_array($fields) ){
  79. foreach( $fields as $field ){
  80. $sql .= $field.', ';
  81. }
  82. }
  83. else{
  84. $sql .= $fields;
  85. }
  86. $sql = rtrim(trim($sql),',');
  87. $sql .= ' FROM '.$table;
  88. # 查询条件
  89. if( !empty($where) ){
  90. $sql .= ' WHERE ';
  91. foreach( $where as $k => $v ){
  92. if( is_array($v) ){
  93. $sql .= '`' .$k. '`' .$v[0]. ':' .$k. ' AND ';
  94. $exe[ $k ] = $v[1];
  95. }
  96. else{
  97. $sql .= '`' .$k. '`=:' .$k. ' AND ';
  98. $exe[ $k ] = $v;
  99. }
  100. }
  101. $sql = rtrim( trim($sql),'AND');
  102. }
  103. $sql .= ';';
  104. return $this->prepare_exe( 'find', $sql, $exe );
  105. }
  106. /**
  107. * 新增
  108. * @param $table
  109. * @param $data 格式['user_name'=>'xx','desc'=>'xx','hobby'=>'xx']
  110. * @return bool
  111. */
  112. public function insert( $table, $data=[] ){
  113. # 创建SQL语句
  114. $exe = [];
  115. $sql = 'INSERT INTO '.$table.' SET ';
  116. # 组装插入语句
  117. if( !empty($data) ){
  118. foreach( $data as $k=>$v ){
  119. $sql .= '`' .$k. '`=:' .$k. ', ';
  120. $exe[ $k ] = $v;
  121. }
  122. $sql .= '`create_time`=:ct;';
  123. $exe[ 'ct' ] = time();
  124. }
  125. else{
  126. return '新增数据不能为空';
  127. }
  128. return $this->prepare_exe( 'insert', $sql, $exe );
  129. }
  130. /**
  131. * 更新
  132. * @param $table
  133. * @param $data 格式['email'=>'xx']
  134. * @return bool
  135. */
  136. public function update($table,$data=[], $where=[]) {
  137. # 创建SQL语句
  138. $exe = [];
  139. $sql = 'UPDATE '.$table.' SET ';
  140. # 组装修改语句
  141. if( !empty($data) ){
  142. foreach( $data as $key=>$val ){
  143. $sql .= '`' .$key.'`=:'.$key.', ';
  144. $exe[ $key ] = $val;
  145. }
  146. $sql .= '`update_time`=:ut';
  147. $exe[ 'ut' ] = time();
  148. }
  149. else{
  150. return '更新数据不能为空';
  151. }
  152. # 查询条件
  153. if( !empty($where) ){
  154. $sql .= ' WHERE ';
  155. foreach( $where as $k => $v ){
  156. if( is_array($v) ){
  157. $sql .= '`' .$k. '`' .$v[0]. ':' .$k. ' AND ';
  158. $exe[ $k ] = $v[1];
  159. }
  160. else{
  161. $sql .= '`' .$k. '`=:' .$k. ' AND ';
  162. $exe[ $k ] = $v;
  163. }
  164. }
  165. $sql = rtrim( trim($sql),'AND');
  166. }
  167. $sql .= ';';
  168. return $this->prepare_exe( 'update', $sql, $exe );
  169. }
  170. /**
  171. * 删除
  172. * @param $table
  173. * @param $where 格式['id'=>['>',5],'name'=>'Jason']
  174. * @return bool
  175. */
  176. public function delete($table,$where=[]){
  177. # 创建SQL语句
  178. $exe = [];
  179. $sql = "DELETE FROM {$table} ";
  180. # 查询条件
  181. if( !empty($where) ){
  182. $sql .= ' WHERE ';
  183. foreach( $where as $k => $v ){
  184. if( is_array($v) ){
  185. $sql .= '`' .$k. '`' .$v[0]. ':' .$k. ' AND ';
  186. $exe[ $k ] = $v[1];
  187. }
  188. else{
  189. $sql .= '`' .$k. '`=:' .$k. ' AND ';
  190. $exe[ $k ] = $v;
  191. }
  192. }
  193. $sql = rtrim(trim($sql), 'AND');
  194. }
  195. $sql .= ';';
  196. return $this->prepare_exe( 'delete', $sql, $exe );
  197. }
  198. /**
  199. * 统计数量
  200. * @param $table
  201. * @param $where 格式['id'=>['>',5],'name'=>'Jason']
  202. * @return number
  203. */
  204. public function count_num($table, $where=[]){
  205. # 创建SQL语句
  206. $exe = [];
  207. $sql = 'SELECT count(*) as count_number FROM '.$table;
  208. # 查询条件
  209. if( !empty($where) ){
  210. $sql .= ' WHERE ';
  211. foreach( $where as $k => $v ){
  212. if( is_array($v) ){
  213. $sql .= '`' .$k. '`' .$v[0]. ':' .$k. ' AND ';
  214. $exe[ $k ] = $v[1];
  215. }
  216. else{
  217. $sql .= '`' .$k. '`=:' .$k. ' AND ';
  218. $exe[ $k ] = $v;
  219. }
  220. }
  221. $sql = rtrim(trim($sql), 'AND');
  222. }
  223. $sql .= ';';
  224. return $this->prepare_exe( 'count', $sql, $exe );
  225. }
  226. /**
  227. * @param $type sql类型
  228. * @param $sql sql模版语句
  229. * @param $exe sql执行条件
  230. * @return bool
  231. */
  232. private function prepare_exe( $type, $sql, $exe ){
  233. # 创建PDO预处理对象
  234. $stmt = $this->pdo->prepare($sql);
  235. # 执行查询操作
  236. if( $stmt->execute( $exe ) ){
  237. if( $stmt->rowCount()>0 ){
  238. switch( $type ){
  239. case $type=='select' || $type=='find':
  240. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  241. if( $type =='select' ) return $stmt->fetchAll(); # 返回一个二维数组
  242. else return $stmt->fetch();
  243. case $type=='insert' || $type=='update' || $type=='delete':
  244. return true;
  245. case 'count':
  246. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  247. $n = $row['count_number'];
  248. return $n;
  249. default:
  250. break;
  251. }
  252. }
  253. }
  254. else{
  255. return false;
  256. }
  257. }
  258. }
  259. # 实例化
  260. $db = new Db('127.0.0.1','SqlTest', 'utf8','root', 'root');
  261. $select = $db->select( 'zsgc',['id','user_name'],['id'=>['>',1],'email'=>['!=','']] ,'id desc');
  262. var_dump( $select );
  263. echo '<hr>';
  264. $find = $db->find( 'zsgc',['id','user_name'],['id'=>['>',1],'email'=>['!=','']] );
  265. var_dump( $find );
  266. echo '<hr>';
  267. //$insert = $db->insert( 'zsgc',['user_name'=>'Bonney1','desc'=>'她很优秀','hobby'=>'滑雪'] );
  268. //print_r( $insert );
  269. //echo '<hr>';
  270. $update = $db->update( 'zsgc',['email'=>'123456@qq.com'],['id'=>27] );
  271. print_r( $update );
  272. echo '<hr>';
  273. $delete = $db->delete( 'zsgc',['id'=>['>',20],'email'=>['!=','']] );
  274. print_r( $delete );
  275. echo '<hr>';
  276. $count = $db->count_num( 'zsgc',['id'=>['>',20]] );
  277. print_r( $count );
  278. echo '<hr>';
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议