Correction status:Uncorrected
Teacher's comments:
<?php echo "<pre>"; //连接PDO function con(){ $dsn = "mysql:host=127.0.0.1;dbname=student;charset=utf8;port=3306"; $dbname = "root"; $dbpwd = "root"; $p = new PDO($dsn,$dbname,$dbpwd); try{ $p = new PDO($dsn,$dbname,$dbpwd); }catch(PDOException $e){ print_r($e->getMessage()); exit; } return $p; } //查询方法 //$table 必须传值 function select($table,$filed='*',$where='',$order='',$limit=''){ //连接 $s = con(); //封装 $sql = 'SELECT '; $sql .= $filed; $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; } //预处理 $a = $s->prepare($sql); //执行sql语句 //判断一下,可能会传一些无法估计的参数,导致查询失败 if($a->execute()){ //查询结果后,结果集可能没有数据 //用rowCount()方法,如果没有数据,返回他一个false if($a->rowCount()){ //设置结果集类型 $a->setFetchMode(PDO::FETCH_ASSOC); //返回结果集的数组 $ret = $a->fetchAll(); //返回结果集 return $ret; }else{ return false; } }else{ return false; } } //print_r(select("stu_user","username,password,age","status=0","age ASC","1,1")); //修改方法 function update($table2,$up=[],$where2=''){ //连接 $u = con(); //封装sql $sql2 = "UPDATE {$table2} SET "; if(is_array($up)){ foreach ($up as $key => $value) { $sql2 .= $key."="."'$value'".","; } } $sql2 = rtrim(trim($sql2),','); if(!empty($where2)){ $sql2 .= ' WHERE '; $sql2 .= $where2; } //预处理 $b = $u->prepare($sql2); //执行sql语句 if($b->execute()){ if($b->rowCount()>0){ return true; } }else{ return false; } } //print_r(update("`stu_user`",array('password'=>'327122','mobile'=>'252'),"id>10")); //添加方法 function insert($table3,$data=[]){ //连接 $i = con(); //封装sql $sql3 = "INSERT INTO {$table3} SET "; if(is_array($data)){ foreach ($data as $key => $value) { $sql3 .= $key."="."'$value'".","; } }else{ die("必须传入数组,array(键名=>键值)"); } $sql3 = rtrim(trim($sql3),','); //预处理 $c = $i->prepare($sql3); print_r($c); if($c->execute()){ echo 111; if($c->rowCount()>0){ return true; } } else { return false; } } //$v = insert("`stu_user`",array('username'=>'hewenlu','password'=>'123','age'=>'18','email'=>'2274597679@qq.com','mobile'=>'13931458554','sex'=>'1','class'=>'双桥','status'=>'1')); //print_r($v); //删除方法 function delete($table4,$where4=''){ //连接 $del = con(); //sql封装 $sql4 = "DELETE FROM {$table4} "; //判断条件是否为空 if(!empty($where4)){ $sql4 .= ' WHERE '; $sql4 .= $where4; } //预处理 $d = $del->prepare($sql4); //执行sql语句 //判断是否执行成功 if($d->execute()){ //判断是否有数据 if($d->rowCount()>0){ return true; } }else{ return false; } } //print_r(delete("`stu_user`","id=16")); //统计数量 function count_num($table5,$where5=''){ //连接 $n = con(); //sql语句 count_number 给到一个字段名,便于输出结果 $sql5 = "SELECT count(*) as count_number FROM {$table5}"; if(!empty($where5)){ $sql5 .= " WHERE {$where5}"; } //预处理 $e = $n->prepare($sql5); //判断执行sql if($e->execute()){ if($e->rowCount()>0){ //转化为数组 $ret_num = $e->fetch(PDO::FETCH_ASSOC); //结果集 $ret_nums = $ret_num['count_number']; return $ret_nums; } }else{ return false; } } print_r(count_num("`stu_user`","class='双桥'"));