<?php echo "<pre>"; //连接PDO function connect(){ $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 = connect(); //封装 $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; } //echo $sql; //sql语句 // $sql = "SELECT FROM `stu_user` WHERE `id` = 1 "; //预处理 $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 = connect(); //封装sql $sql2 = 'UPDATE '; $sql2 .= $table2; $sql2 .= ' SET '; $sql2 .= $up; if(!empty($where2)){ $sql2 .= ' WHERE '; $sql2 .= $where2; } //sql语句 //$sql2 = "UPDATE `stu_user` SET `age`= 21 WHERE `id` = 1"; //预处理 $b = $u->prepare($sql2); //执行sql语句 if($b->execute()){ if($b->rowCount()){ //设置结果集类型 $b->setFetchMode(PDO::FETCH_ASSOC); //返回输出结果集 $ret2 = $b->fetchAll(); return $ret2; }else{ return false; } }else{ return false; } } //print_r(update("stu_user","password=327122","id=1")); //添加方法 function insert($table3,$username='',$password='',$age='',$email='',$mobile='',$sex='',$adress='',$class=''){ //连接 $i = connect(); //封装sql $sql3 = 'INSERT INTO '; $sql3 .= $table3; $sql3 .= ' (`username`,`password`,`age`,`email`,`mobile`,`sex`,`adress`,`class`)'; $sql3 .= ' VALUES '; $sql3 .= '('; if(!empty($username)){ $sql3 .= "'$username'".","; } if(!empty($password)){ $sql3 .= "'$password'".","; } if(!empty($age)){ $sql3 .= "$age".","; } if(!empty($email)){ $sql3 .= "'$email'".","; } if(!empty($mobile)){ $sql3 .= "'$mobile'".","; } if(!empty($sex)){ $sql3 .= "$sex".","; } if(!empty($adress)){ $sql3 .= "'$adress'".","; } if(!empty($class)){ $sql3 .= "'$class'".","; } //$sql3 .= "'$password'".",".$age.$email.$mobile.$sex.$class.$status; // $sql3 .= $username.",".$password.",".$age.",".$email.",".$mobile.",".$sex.",".$class.",".$status; $sql3 = rtrim($sql3,","); $sql3 .= ')'; print_r($sql3); //sql语句 //$sql3 = "INSERT INTO `stu_user` (username,password,age,email,mobile,sex,adress,class ) VALUES ('pdo','pdo123','10','pdo@qq.com','123456789','1 ','php第五期','高二' )"; //预处理 $c = $i->prepare($sql3); //执行sql语句 $c->execute(); //设置结果集类型 $c->setFetchMode(PDO::FETCH_ASSOC); //结果集 $ret3 = $c->fetchAll(); //返回输出结果集 return $ret3; } // print_r(insert("stu_user","hewenlu","1234","15","252@qq.com","13931458554","1","赛奇","0")); $v = insert("`stu_user`","hewenlu","123","18","1","111@qq.com","11111111","朝阳","高三"); //print_r($v); //删除方法 function delete($table4,$where4=''){ //连接 $del = connect(); //sql封装 $sql4 = 'DELETE FROM '; $sql4 .= $table4; //判断条件是否为空 if(!empty($where4)){ $sql4 .= ' WHERE '; $sql4 .= $where4; } //sql语句 //$sql4 = "DELETE FROM `stu_user` WHERE id>12"; //预处理 $d = $del->prepare($sql4); //执行sql语句 //判断是否执行成功 if($d->execute()){ //判断是否有数据 if($d->rowCount()){ //设置结果集类型 $d->setFetchMode(PDO::FETCH_ASSOC); //结果集 $ret4 = $d->fetchAll(); //返回结果集 return $ret4; }else{ return false; } }else{ return false; } } print_r(delete("stu_user","id=12"));