Correcting teacher:查无此人
Correction status:qualified
Teacher's comments:完成的不错,继续加油。
<?php
$db = [
'type' => 'mysql',
'host' => 'localhost',
'dbname' => 'movies',
'username' => 'root',
'password' => 'root'
];
//配置数据源DSN信息 注意不能有空格
$dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
try{
//连接数据库
$pdo = new PDO($dsn,$db['username'],$db['password']);
}catch(PDOException $e){
//输出错误信息
die($e->getMessage());
}
//1,准备SQL语句 :n 是占位符
$sql ='INSERT INTO `category` SET `name` = :n,`alias` = :alias';
//2,创建预处理对象
$stmt = $pdo -> prepare($sql);
//3,绑定数据
$n = 'gt';
$alias = '港台大片';
$stmt->bindParam('n',$n,PDO::PARAM_STR);
//4,执行sql操作
$add = $stmt->execute();
//5,判断受影响的行数
if($add){
if($stmt->rowCount() > 0){
echo '成功新增了'.$stmt->rowCount().'条记录';
}
}else{
die(print_r($stmt->errorInfo(),true));
}
//6,关闭PDO连接
$pdo = null;
$sql = 'DELETE FROM `category` WHERE `cate_id` = :id';
$stmt = $pdo->prepare($sql);
$id = 4;
$stmt->bindParam('id',$id,PDO::PAMARM_INT);
$del = $stmt->execute();
if($del){
if($stmt->rowCount() >0){
echo '成功删除了'..$stmt->rowCount().'条记录';
}
}else{
die(print_r($stmt->errorInfo(),true));
}
$sql = 'UPDAATE `category` SET `name` = :n,`alias` = :alias WHERE `cate_id` = :id';
$stmt = $pdo->prepare($sql);
$n = 'xmt';
$alias = '新马泰大片';
$cate_id = 4;
$stmt->bindParam('n',$n,PDO::PARAM_STR);
$stmt->bindParam('alias',$alias,PDO::PARAM_STR);
$stmt->bindParam('id',$id,PDO::PARAM_INT);
$res = $stmt->execute();
if($res){
if($stmt->rowCount() >0){
echo '成功修改了'.$stmt->rowCount().'条记录';
}
}else{
die(print_r($stmt->errorInfo(),true));
}
$sql = 'SELECT * FROM `category` WHERE `cate_id` = :id';
$stmt = $pdo -> prepare($sql);
$id=4;
$stmt->bindParam('id',$id,PDO::PARAM_INT);
$sel = $stmt->execute();
//fetch() 获取结果集中的一行
//PDO::FETCH_ASSOC 获取关联数组部分
while($a = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo print_r($a,true);
}
//获取结果集
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) AS $V){
echo print_r($v,true);
}