<?php //1.创建PDO对象,连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //2 创建SQL语句 $sql = "UPDATE `user` SET `email` = :email, `create_time` = :create_time WHERE `id` = :id"; //3.验证SQL语句,创建出预处理对象 $stmt = $pdo->prepare($sql); //4.参数绑定 $id = 3; $email = 'lwt@php.cn'; $createTime = time(); $stmt->bindParam(':id',$id,PDO::PARAM_INT); $stmt->bindParam(':email',$email,PDO::PARAM_STR,100); $stmt->bindParam(':create_time',$createTime,PDO::PARAM_INT); //5.执行更新 if($stmt->execute()){ echo ($stmt->rowCount()>0) ? '成功更新了 '.$stmt->rowCount().' 条记录!' : '没有记录被更新'; }else{ exit(print_r($stmt->errorInfo(),true)); } insert <?php //1.创建PDO对象,连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //2 创建SQL语句 $sql = "INSERT INTO `user` (`name`,`email`,`password`,`status`,`create_time`) VALUES (:name,:email,:password,:status,:create_time) "; //3.验证SQL语句,创建出预处理对象 $stmt = $pdo->prepare($sql); //4.参数绑定 $name = '东方不败'; $email = 'dfbb@php.cn'; $password = 'sha1(123456)'; $status = 1; $createTime = time(); $stmt->bindParam(':name',$name,PDO::PARAM_STR,20); $stmt->bindParam(':email',$email,PDO::PARAM_STR,100); $stmt->bindParam(':password',$password,PDO::PARAM_STR,40); $stmt->bindParam(':status',$status,PDO::PARAM_INT); $stmt->bindParam(':create_time',$createTime,PDO::PARAM_INT); //5.执行添加 if($stmt->execute()){ echo ($stmt->rowCount()>0) ? '成功添加了 '.$stmt->rowCount().' 条记录!' : '没有记录被添加'; }else{ exit(print_r($stmt->errorInfo(),true)); } delete <?php //创建PDO对象 连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //创建SQL语句 $sql = 'DELETE FROM `user` WHERE `id` = :id'; //3. 验证SQL语句,创建出预处理对象 $stmt = $pdo->prepare($sql); //4.参数绑定 $id = 1; $stmt->bindParam(':id',$id,PDO::PARAM_INT); //5.执行删除 if($stmt->execute()){ echo ($stmt->rowCount()>0) ? '成功删除了' .$stmt->rowCount().'条记录!' : '没有记录被删除'; }else{ exit(print_r($stmt->errorInfo(),true)); }