Correcting teacher:查无此人
Correction status:qualified
Teacher's comments:完成的不错,继续加油。
PDO: PHP data objection.类似一个中间层,能对各种数据库进行操作,例如对mysql,就不用
mysql()和mysqli()函数操作了,php5以上自带。
连接数据库操作:
$dbType = 'mysql';//数据库类型
$host = 'localhost';//主机名
$dbName = 'anguoguo';//默认数据库
$userName = 'root';
$password = 'root';
$dsn = "{$dbType}:host={$host};dbname={$dbName}";
try{
$pdo = new PDO($dsn,$userName,$password);
echo '连接成功';
}catch (PDOException $e){
die('Connection Failed:' . $e->getMessage());
}
增:
$sql = 'INSERT INTO `system` SET `sys_id`=:sys_id,`title`=:title,`desc`=:desc,`key`=:key,`copy`=:copy';
//sql语句模板
$stmt = $pdo->prepare($sql);
//sql语句(statement)对象--预处理,生成预处理对象PDOStatement
$sys_id = 1;
$title = '私人影院';
$desc = '收集全网最新最全的影视资源';
$key = '国产,欧美,日韩';
$copy = '欧阳克';//根据前面占位符的名字添加变量,并赋值
$stmt->bindParam('sys_id',$sys_id,PDO::PARAM_INT);
$stmt->bindParam('title', $title, PDO::PARAM_STR);
$stmt->bindParam('desc', $desc, PDO::PARAM_STR);
$stmt->bindParam('key', $key, PDO::PARAM_STR);
$stmt->bindParam('copy', $copy, PDO::PARAM_STR);
if($stmt->execute()){ //执行SQL操作
if($stmt->rowCount()>0){
echo '成功添加' . $stmt->rowCount() . '条记录,该记录的主键id是:' . $pdo->lastInsertId();
}
}else{
die('<pre>' . print_r($stmt->errorInfo(),true));
}
echo '释放后数组为什么会出现';
unset($PDO);
删
// 创建SQL语句模板
$sql = 'DELETE FROM `category` WHERE `cate_id`=:cate_id';
// 创建SQL语句对象
$stmt = $pdo->prepare($sql);
// 将变量绑定到SQL语句模板的命名占位符上
$cate_id = 4;
$stmt->bindParam('cate_id', $cate_id, PDO::PARAM_INT);
// 执行SQL操作
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
echo '成功删除了' . $stmt->rowCount() . '条记录';
}
} else {
die('<pre>' . print_r($stmt->errorInfo(), true));
}
// 销毁pdo, 关闭连接
$pdo = null;
查
$sql = 'SELECT * FROM `movies` WHERE `cate_id` = :cate_id';
$stmt = $pdo->prepare($sql);
$cate_id = 1;
$stmt->bindParam('cate_id',$cate_id,PDO::PARAM_INT);
//绑定占位符和执行操作顺序改变
$stmt->execute();
while($movie = $stmt->fetch(PDO::FETCH_ASSOC)){
echo '<pre>' . print_r($movie,true);
}
$pdo = null;
//fetchAll()
require 'pdo_config.php';
$sql = 'SELECT * FROM `movies` WHERE `cate_id` = :cate_id';
$stmt = $pdo->prepare($sql);
$cate_id = 3;
$stmt->bindParam('cate_id',$cate_id,PDO::PARAM_INT);
$stmt->execute();
$movies = $stmt ->fetchAll(PDO::FETCH_ASSOC);
foreach($movies as $movie){
echo '<pre>' . print_r($movies,true);
}
unset($pdo);
//bindColumn()
require 'pdo_config.php';
echo '<br>';
$sql = 'SELECT * FROM `movies` WHERE `cate_id` =:cate_id';
$stmt = $pdo->prepare($sql);
$cate_id = 3;
$stmt->bindParam('cate_id',$cate_id,PDO::PARAM_INT);
$stmt->execute();
$stmt->bindColumn('name',$name);
$stmt->bindColumn('detail',$detail);
while ($stmt->fetch(PDO::FETCH_ASSOC)){
$detail = mb_substr($detail,0,20,'utf-8');
echo '片名:' . $name . '<br>' . '简介:' . $detail . '...<hr>';
}
//while是循环,一直到行没有数据为止
$pdo = null;
改:
$sql = 'UPDATE `category` SET `name` = :n, `alias` = :alias WHERE `cate_id`=:cate_id';
$stmt = $pdo->prepare($sql);
$name = 'xjj';
$alias = '小姐姐';
$cate_id = 4;
//确保有这条记录
$stmt->bindParam('n',$name,PDO::PARAM_STR);
//::静态常量,->对外开放的类
$stmt->bindParam('alias', $alias, PDO::PARAM_STR);
$stmt->bindParam('cate_id', $cate_id, PDO::PARAM_INT);
if($stmt->execute()){
if($stmt->rowCount()>0){
echo '成功更新了' . $stmt->rowCount() . '条记录';
}
}else{
die('<pre>' . print_r($stmt->errorInfo(),true));
}
$pdo = null;
print_r($pdo);