Correction status:qualified
Teacher's comments:
<?php if(!function_exists('connect' )){ function conncet($dbname,$type='mysql',$host='127.0.0.1',$charset='utf8',$port=3306,$user='root',$pass='root'){ $dsn = "{$type}:host={$host};dbname={$dbname};charset={$charset};port={$port}"; $userName = $user;//数据库用户名 $password = $pass;//数据库用户密码 $options = [//配置连接属性 PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,//错误模式 PDO::ATTR_CASE=>PDO::CASE_NATURAL,//自然名称 PDO::ATTR_EMULATE_PREPARES=>TRUE,//弃用模拟功能 PDO::ATTR_PERSISTENT=>TRUE, ]; try{//实例化PDO类,创建PDO对象 $pdo =new PDO($dsn,$userName,$password,$options); }catch(PDOException $e){ print '连接错误'.$e->getMessage(); die(); } return $pdo; } }//连接数据库 if(!function_exists('insert' )){//检查指定的函数是否已经定义 function insert($pdo,$table,$data=[]){//建立insert函数并设置三个参数 $sql="INSERT IGNORE {$table} SET "; foreach(array_keys($data) as $field){//循环添加$data数组中的数据 $sql .=$field.'=:'.$field.','; } $sql =rtrim(trim($sql),',').';';//创建sql语句 $stmt = $pdo->prepare($sql);//创建stmt对象 foreach($data as $field=>$value){//绑定参数到预处理 $stmt->bindValue(":{$field}",$value); } //die($stmt->queryString);//检查sql语句是否错误 if($stmt->execute()){//执行新增操作 if($stmt->rowCount()>0){//判断执行是否成功并返回布尔值 return true; } }else{ return false; } } }//新增数据 if(!function_exists('update' )){ function update($pdo,$table,$data=[],$where ='' ){ //创建sql语句 $sql="UPDATE {$table} SET "; foreach(array_keys($data) as $field){ $sql .=$field.'=:'.$field.','; } $sql =rtrim($sql,","); IF(!empty($where)){ $sql .=' WHERE '.$where.';'; }else{ exit('条件不能为空'); } $stmt = $pdo->prepare($sql);//创建stmt对象 foreach($data as $field=>$value){//绑定参数到预处理 $stmt->bindValue(":{$field}",$value); } if($stmt->execute()){//执行新增操作 if($stmt->rowCount()>0){ return true; } }else{ return false; } } }//更新数据 if(!function_exists('find' )){ function find($pdo,$table,$fields,$where ='' ){ //创建sql语句 $sql="SELECT"; if(is_array($fields)){ foreach($fields as $field){ $sql .=$field.','; } }else{ $sql .=$fields.','; } $sql =rtrim(trim($sql),','); $sql .=' FROM '.$table; if(!empty($where)){ $sql .=' WHERE '.$where; } $sql .=' LIMIT 1 ;'; $stmt = $pdo->prepare($sql); if($stmt->execute()){ if($stmt->rowCount()>0){ $stmt->setFetchMode(PDO::FETCH_ASSOC); return $stmt->fetch(); } }else{ return false; } } }//查询单条数据 if(!function_exists('select' )){ function select($pdo,$table,$fields,$where ='' ){ //创建sql语句 $sql="SELECT"; if(is_array($fields)){ foreach($fields as $field){ $sql .=$field.','; } }else{ $sql .=$fields.','; } $sql =rtrim(trim($sql),','); $sql .=' FROM '.$table; if(!empty($where)){ $sql .=' WHERE '.$where; } if(!empty($order)){ $sql .=' ORDER BY '.$order; } //die($sql);//输出sql语句查看错误 $stmt = $pdo->prepare($sql); //die($stmt->queryString); if($stmt->execute()){ if($stmt->rowCount()>0){ $stmt->setFetchMode(PDO::FETCH_ASSOC); return $stmt->fetchAll(); } }else{ return false; } } }//查询多条数据 if(!function_exists('delete')){ function delete($pdo,$table, $where ='' ){ //创建sql语句 $sql="DELETE FROM {$table}"; IF(!empty($where)){ $sql .=' WHERE '.$where.';'; }else{ exit('条件不能为空'); } $stmt = $pdo->prepare($sql);//创建stmt对象 //die($stmt->queryString); if($stmt->execute()){//执行删除操作 if($stmt->rowCount()>0){ return true; } }else{ return false; } } }//删除数据 ?>
以上为封装操作,组装sql语句需要不断使用die($stmt->queryString);//检查sql语句错误
<?php require 'func_pdo.php'; $dbname='php'; $type='mysql'; $host='127.0.0.1'; $charset='utf8'; $port=3306; $user='root'; $pass='root'; $pdo=conncet($dbname,$type,$host,$charset,$port,$user,$pass); ////新增测试 //$table = 'staff'; //$data = ['name'=>'PF','sex'=>0,'age'=>30,'salary'=>888]; //insert($pdo,$table,$data); // ////更新测试 //$table = 'staff'; //$data = ['name'=>'PFGT','sex'=>0,'age'=>30,'salary'=>999]; //$where = 'staff_id=21'; //update($pdo,$table,$data,$where); ////单条查询测试 //$table = 'staff'; //$fields = ['name','sex','age','salary']; //$fields = '*'; //$fields = ' name, salary'; //$where ='age<40'; //echo '<pre>'.print_r(find($pdo,$table,$fields,$where),true).'</pre>'; ////多条查询测试 //$table = 'staff'; //$fields = ['name','age','salary']; //$fields = '*'; //$fields = ' name, salary'; //$where ='age<20'; //$order ='age ASC'; //echo '<pre>'.print_r(select($pdo,$table,$fields,$where,$order),true).'</pre>'; //删除测试 $table = 'staff'; $where = 'staff_id=21'; delete($pdo,$table,$where); ?>
此为测试提交页面