abstract:新增操作 <?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); $sql = "INSERT INTO `userinfo`(`name`, `sex`,&
新增操作 <?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); $sql = "INSERT INTO `userinfo`(`name`, `sex`, `age`, `email`, `password`, `status`, `create_time`) VALUES (:name,:sex,:age,:email,:password,:status,:create_time)"; $stmt = $pdo->prepare($sql); //参数绑定 $name ='超人'; $sex='男'; $age=30; $email ='12345@qq.com'; $password=sha1('654321'); $status=1; $create_time=time(); $stmt->bindParam(':name',$name,PDO::PARAM_STR,20); $stmt->bindParam(':sex',$sex,PDO::PARAM_STR,20); $stmt->bindParam(':age',$age,PDO::PARAM_INT); $stmt->bindParam(':email',$email,PDO::PARAM_STR,20); $stmt->bindParam(':password',$password,PDO::PARAM_STR,40); $stmt->bindParam(':status',$status,PDO::PARAM_INT); $stmt->bindParam(':create_time',$create_time,PDO::PARAM_INT); if($stmt->execute()){ echo ($stmt->rowCount()>0)?'成功添加了'.$stmt->rowCount().'条记录!':'没有记录被添加~~~'; }else{ exit(print_r($stmt->errorInfo(),true)); }
更新操作 <?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); $sql = "UPDATE `userinfo` SET `email`=:email,`create_time`=:create_time WHERE `ID` = :id "; $stmt = $pdo->prepare($sql); //参数绑定 $id =2; $email ='1234554321@qq.com'; $create_time=time(); $stmt->bindParam(':id',$id,PDO::PARAM_INT); $stmt->bindParam(':email',$email,PDO::PARAM_STR,20); $stmt->bindParam(':create_time',$create_time,PDO::PARAM_INT); if($stmt->execute()){ echo ($stmt->rowCount()>0)?'成功更新了'.$stmt->rowCount().'条记录!':'没有记录被更新~~~'; }else{ exit(print_r($stmt->errorInfo(),true)); }
删除操作 <?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); $sql = "DELETE FROM `userinfo` WHERE `ID` = :id "; $stmt = $pdo->prepare($sql); //参数绑定 $id =3; $stmt->bindParam(':id',$id,PDO::PARAM_INT); if($stmt->execute()){ echo ($stmt->rowCount()>0)?'成功删除了'.$stmt->rowCount().'条记录!':'没有记录被删除~~~'; }else{ exit(print_r($stmt->errorInfo(),true)); }
Correcting teacher:查无此人Correction time:2019-05-06 09:36:21
Teacher's summary:完成的不错。可以把常用的写成方法,直接调用就可以了。继续加油。