Correction status:qualified
Teacher's comments:
<?php /** * PDO新增数据 */ //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\'')); //创建预处理对象 $stmt = $pdo->prepare("INSERT `user` SET `name`= :name , `email`= :email, `password`= sha1(:password)"); //执行 $stmt -> execute(['name'=>'李林','email'=>'lilin@126.cn','password'=>'123456']); echo '成功添加了:'.$stmt->rowCount().'条数据';
点击 "运行实例" 按钮查看在线实例
<?php /** * pdo删除操作 */ //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\'')); //创建预处理对象 $stmt = $pdo->prepare('DELETE FROM `user` WHERE `id` = :id'); //执行 if ($stmt->execute(['id'=>4])){ echo '成功删除:'.$stmt->rowCount().'条数据'; }else{ exit($stmt->errorInfo()); } $stmt = null; $pdo = null;
点击 "运行实例" 按钮查看在线实例
<?php /** * pdo更新数据 */ $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\'')); $stmt = $pdo->prepare('UPDATE `user` SET `name`=:name,`email`=:email WHERE `id`=:id'); $stmt->execute(['id'=>8,'name'=>'admin','email'=>'qq@qq.com']); echo '成功更新'.$stmt->rowCount().'条数据';
点击 "运行实例" 按钮查看在线实例
<?php /** * pdo查询数据 */ $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\'')); $stmt = $pdo->prepare('SELECT `id`,`name`,`email` FROM `user` WHERE `id`>:id'); $stmt->execute(['id' => 1]); //将结果集绑定到变量上 $stmt->bindColumn('id',$id); $stmt->bindColumn('name',$name); $stmt->bindColumn('email',$email); //循环输出结果 while($stmt->fetch(PDO::FETCH_BOUND)){ echo 'id:'.$id.' 姓名:'.$name.' 邮箱:'.$email ,'<br>'; }
点击 "运行实例" 按钮查看在线实例