Correction status:qualified
Teacher's comments:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>PDO操作数据库</title> <style type="text/css"> table{ width:500px; border-collapse:collapse; margin-top:20px; } table tr th{ border:1px solid black; padding:5px; text-align:center; font-size:20px; } table tr:first-child{ background:lightblue; font-weight:bold; } table tr td{ border:1px solid black; padding:5px; text-align:center; } </style> </style> </head> <body> <?php //PDO:PHP数据对象,是PHP操作所有数据库的抽象层,提供了一个统一的访问接口 //抛出异常 try{ //1.实例化一个PDO对象并连接数据库new PDO($dns,$user,$password),$dsn:数据源,设置数据库类型,以及数据库服务器和默认数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=test','root','123456'); //2.要执行的SQL语句 IGNORE:防止重复插入 :user占位符 //插入数据 $sql = 'INSERT IGNORE cms_user SET user=:user,pass=sha1(:pass),email=:email'; //3.创建预处理对象 $stmt = $pdo->prepare($sql); $data = array('user'=>'柯南','pass'=>'123456','email'=>'kenan@qq.com'); //4.绑定参数 $stmt->bindParam(':user',$data['user'],PDO::PARAM_STR); //PDO::PARAM_STR,常量,说明参数为字符串型 $stmt->bindParam(':pass',$data['pass']); $stmt->bindParam(':email',$data['email']); if($stmt->execute()){ //rowCount(),返回执行所影响的行数,一般是写(增,删,改)操作 echo '成功插入'.$stmt->rowCount().'条数据.<br>'; }else{ echo '<sapn style="color:red">插入失败!</span>'; print_r($stmt->errorInfo()); //打印错误信息 exit(); } //简化参数绑定 $data = array('user'=>'蓝猫','pass'=>'123456','email'=>'lanmao@php.com'); if($stmt->execute($data)){ //rowCount(),返回执行所影响的行数,一般是写(增,删,改)操作 echo '成功插入'.$stmt->rowCount().'条数据.<br>'; }else{ echo '<sapn style="color:red">插入失败!</span>'; print_r($stmt->errorInfo()); //打印错误信息 exit(); } $sql = 'UPDATE IGNORE `cms_user` SET email=:email WHERE user="蜡笔小新"'; $stmt = $pdo->prepare($sql); $email= array('email'=>'123@123.com'); if($stmt->execute($email)){ echo '成功插入'.$stmt->rowCount().'条数据.<br>'; }else{ echo '<sapn style="color:red">修改失败!</span>'; print_r($stmt->errorInfo()); //打印错误信息 exit(); } $sql = 'SELECT id,user,email FROM `cms_user` WHERE id>:id'; $stmt = $pdo->prepare($sql); if($stmt->execute(array('id'=>20))){ //fetchAll获取所有结果集,返回值是二维数组,数据量大时不推荐使用 echo '<table><tr><th>id</th><th>user</th><th>email</th></tr>'; foreach($rows=$stmt->fetchAll() as $row){ echo '<tr><td>'.$row['id'].'</td><td>'.$row['user'].'</td><td>'.$row['email'].'</td></tr>'; } echo '<table>'; }else{ echo '<sapn style="color:red">查询失败!</span>'; print_r($stmt->errorInfo()); //打印错误信息 exit(); } if($stmt->execute(array('id'=>20))){ //fetch()一次取一条数据, echo '<table><tr><th>id</th><th>user</th><th>email</th></tr>'; while($row = $stmt->fetch()){ //$stmt->fetch(PDO::FETCH_ASSOC) ,默认关联索引数组都有,PDO::FETCH_ASSOC只是关联数组 echo '<tr><td>'.$row['id'].'</td><td>'.$row['user'].'</td><td>'.$row['email'].'</td></tr>'; } echo '<table>'; }else{ echo '<sapn style="color:red">查询失败!</span>'; print_r($stmt->errorInfo()); //打印错误信息 exit(); } $sql = 'SELECT count(*) FROM `cms_user`'; $stmt = $pdo->prepare($sql); if($stmt->execute()){ //fetchColumn()获取一行一列,无法获取同一行其他列 echo '总共查询到'.$stmt->fetchColumn().'条数据'; }else{ echo '<sapn style="color:red">查询失败!</span>'; print_r($stmt->errorInfo()); //打印错误信息 exit(); } }catch (PDOException $e){ //抛出异常,输出异常信息 echo '<sapn style="color:red">'.$e->getMessage().'</span>'; exit(); } //释放预处理 $stmt = null; //释放连接或者unset($pdo) $pdo = null; ?> </body> </html>
点击 "运行实例" 按钮查看在线实例