实例
用刚学到的PDO知识封装增删改查函数,可以传入参数
<?php /** * @Author: Marte * @Date: 2019-03-22 11:42:48 * @Last Modified by: Marte * @Last Modified time: 2019-03-22 22:49:53 */ function con(){ $sql = 'mysql'; $host = '127.0.0.1'; $dbName = 'test'; $user = 'sunliang'; $psw = '123456'; try{ $pdo = new PDO("$sql:host=$host;dbname=$dbName",$user,$psw); }catch(PDOException $e){ echo 'connect error: '.$e->getMessage(); } return $pdo; } /** *数据查找函数 selectAll() *$table str 要查找的表名 *$field str 要查找的字段,默认全部查找 *$where str 要查找的条件 *$order str 排序 *$limit str 需要查找的条数 */ function selectAll($table,$field='*',$where="",$order="",$limit=''){ //链接数据库 $pdo = con(); //组合sql语句 //$sql = 'select * from user where age > 20 order by id desc limit 1'; $sql = 'select '; //如果字段不为空,执行 if(!empty($field)){ $sql .= trim($field); } //如果数据表不为空 if(!empty($table)){ $sql .= ' from '; $sql .= $table; } //如果条件不为空 if(!empty($where)){ $sql .= ' where '; $sql .= $where; } //如果排序不为空 if(!empty($order)){ $sql .= ' order by '; $sql .= $order; } //如果limit不为空 if(!empty($limit)){ $sql .= ' limit '; $sql .= $limit; } //echo $sql.'<br/>'; $stmt = $pdo->prepare($sql); if($stmt->execute()){ if($stmt->rowCount()){ $res = $stmt->fetchAll(PDO::FETCH_ASSOC); return $res; }else{ return '没有查找到数据啊'; } }else{ return 'sql语句执行失败'; } } //print_r(selectAll('user','id,name,age,address','age>20','id desc','3')); /** *数据添加函数 adddata() *$table str 要添加的表名 *$field str 要添加的字段 *$values str 要添加的字段值 *return true或false */ function adddata($table,$field,$values){ $pdo = con(); $sql = "insert into "; //检查表名是否为空 if(!empty($table)){ $sql .= $table; }else{ return '没有表名添加不了'; } //检查字段名是否为空 if(!empty($field)){ $sql .= " ($field)"; }else{ return '字段名不能为空'; } $sql .= " values "; //检查值是否为空 if(!empty($values)){ $sql .= " ($values) "; }else{ return '值不能为空'; } //echo $sql;exit; $stmt = $pdo->prepare($sql); if($stmt->execute()){ if($stmt->rowCount()){ return true; }else{ return false; } } } //adddata('user','`name`,`password`,`age`,`email`,`birthday`,`salary`,`height`,`weight`,`address`,`hobby`,`job`',"'黎明','827ccb0eea8a706c4c34a16891f84e7b','48','234234@qq.com','474938751','50000','180','75','中国***','演戏','演员'"); /** *删除数据函数 adddata() *$table str 要添加的表名 *$field str 要添加的字段 *$values str 要添加的字段值 *return true或false */ function del($table,$where){ $pdo = con(); if(empty($table) or empty($where)){ echo '表名和删除条件都不能为空'; exit; } //$sql = 'delete from user where id = 1'; $sql = "delete from "; $sql .= $table; $sql .=" where "; $sql .= $where; $stmt = $pdo->prepare($sql); if($stmt->execute()){ if($stmt->rowCount()){ return true; }else{ return false; } } } //del('user','age>20'); /** *删除数据函数 adddata() *$table str 要添加的表名 *$field str 要添加的字段 *$values str 要添加的字段值 *return true或false */ function update($table,$data,$where){ $pdo = con(); if(empty($table) or empty($where)){ echo '表名和删除条件都不能为空'; exit; } //$sql = 'update user set name="刘德华",age="30",address="中国***" where id=1'; $sql = "update "; $sql .= $table; $sql .=" set "; foreach($data as $k=>$v){ $sql .= $k.'='.'"'.$v.'"'.','; } $sql = rtrim($sql,','); $sql .= " where "; $sql .= $where; //echo $sql;exit; $stmt = $pdo->prepare($sql); if($stmt->execute()){ if($stmt->rowCount()){ return true; }else{ return false; } } } //update('user',['name'=>'刘德华1','age'=>45,'address'=>'中国***'],'id=1');
运行实例 »
点击 "运行实例" 按钮查看在线实例