Correcting teacher:查无此人
Correction status:qualified
Teacher's comments:完成的不错,继续加油。
<?php $db=[ 'type' => 'mysql', 'host' => 'localhost', 'dbname' => 'shy', 'username' => 'root', 'password' => 'root' ]; $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; try{ $pdo = new PDO($dsn,$db['username'],$db['password']); print_r($pdo); }catch (PDOException $e){ die('错误提示:'.$e->getMessage()); }
结果:
手抄:
<?php require 'pdo2.php'; $sql = 'INSERT INTO `system` SET `title`=:t,`desc`=:d,`key`=:k,`copy`=:c'; $stmt = $pdo->prepare($sql); $title = '影视中心'; $desc = '全网新鲜资源'; $key = '影视'; $copy = 'shy'; $stmt ->bindParam('t',$title,PDO::PARAM_STR); $stmt ->bindParam('d',$desc,PDO::PARAM_STR); $stmt ->bindParam('k',$key,PDO::PARAM_STR); $stmt ->bindParam('c',$copy,PDO::PARAM_STR); //结果执行 if ($stmt ->execute()){ if ($stmt -> rowCount()>0){ echo '成功增加'.$stmt -> rowCount().'条记录'; } } else{ die(print_r($stmt->errorInfo(), true)); } //关闭连接 $pdo = null;
效果:
手抄:
<?php require 'pdo2.php'; $sql = 'DELETE FROM `makeup`WHERE `id`=:id'; $stmt = $pdo->prepare($sql); $id = 7; $stmt ->bindParam('id',$id,PDO::PARAM_INT); if ($stmt ->execute()){ if ($stmt -> rowCount()>0){ echo '成功删除'.$stmt -> rowCount().'条记录'; } } else{ die(print_r($stmt->errorInfo(), true)); } //关闭连接 $pdo = null;
结果:
手抄:
<?php require 'pdo2.php'; $sql = 'UPDATE `makeup`SET `price`=:p,`kucun`=:k WHERE `id`=:id'; $stmt = $pdo->prepare($sql); $price = 150; $kucun = 130; $id = 1; $stmt ->bindParam('p',$price,PDO::PARAM_STR); $stmt ->bindParam('k',$kucun,PDO::PARAM_INT); $stmt ->bindParam('id',$id,PDO::PARAM_INT); //结果执行 if ($stmt ->execute()){ if ($stmt -> rowCount()>0){ echo '成功更新'.$stmt -> rowCount().'条记录'; } } else{ die(print_r($stmt->errorInfo(), true)); } //关闭连接 $pdo = null;
结果:
手抄:
<?php require 'pdo2.php'; $sql = 'SELECT * FROM `shoes` WHERE `id`=:id'; $stmt = $pdo->prepare($sql); $id = 3; $stmt ->bindParam('id',$id,PDO::PARAM_INT); $stmt ->execute(); $shoes = $stmt -> fetchAll(PDO::FETCH_ASSOC); foreach ($shoes as $s){ echo '查询'.print_r($s, true); } //关闭连接 $pdo = null;
结果:
手抄:
PDO操作数据库的步骤
1.PDO连接对象;
2.创建语句模板;
3.预处理;
4.绑定参数;
5.执行SQL语句;
6.关闭连接。