Blogger Information
Blog 27
fans 0
comment 0
visits 17371
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
019-11月21日-PHP第九节-PDO
冇忉丼
Original
589 people have browsed it

pdo操作:连接、增删查改操作(手写)

PDO: PHP data objection.类似一个中间层,能对各种数据库进行操作,例如对mysql,就不用
mysql()和mysqli()函数操作了,php5以上自带。
连接数据库操作:

  1. $dbType = 'mysql';//数据库类型
  2. $host = 'localhost';//主机名
  3. $dbName = 'anguoguo';//默认数据库
  4. $userName = 'root';
  5. $password = 'root';
  6. $dsn = "{$dbType}:host={$host};dbname={$dbName}";
  7. try{
  8. $pdo = new PDO($dsn,$userName,$password);
  9. echo '连接成功';
  10. }catch (PDOException $e){
  11. die('Connection Failed:' . $e->getMessage());
  12. }

增:

  1. $sql = 'INSERT INTO `system` SET `sys_id`=:sys_id,`title`=:title,`desc`=:desc,`key`=:key,`copy`=:copy';
  2. //sql语句模板
  3. $stmt = $pdo->prepare($sql);
  4. //sql语句(statement)对象--预处理,生成预处理对象PDOStatement
  5. $sys_id = 1;
  6. $title = '私人影院';
  7. $desc = '收集全网最新最全的影视资源';
  8. $key = '国产,欧美,日韩';
  9. $copy = '欧阳克';//根据前面占位符的名字添加变量,并赋值
  10. $stmt->bindParam('sys_id',$sys_id,PDO::PARAM_INT);
  11. $stmt->bindParam('title', $title, PDO::PARAM_STR);
  12. $stmt->bindParam('desc', $desc, PDO::PARAM_STR);
  13. $stmt->bindParam('key', $key, PDO::PARAM_STR);
  14. $stmt->bindParam('copy', $copy, PDO::PARAM_STR);
  15. if($stmt->execute()){ //执行SQL操作
  16. if($stmt->rowCount()>0){
  17. echo '成功添加' . $stmt->rowCount() . '条记录,该记录的主键id是:' . $pdo->lastInsertId();
  18. }
  19. }else{
  20. die('<pre>' . print_r($stmt->errorInfo(),true));
  21. }
  22. echo '释放后数组为什么会出现';
  23. unset($PDO);

  1. // 创建SQL语句模板
  2. $sql = 'DELETE FROM `category` WHERE `cate_id`=:cate_id';
  3. // 创建SQL语句对象
  4. $stmt = $pdo->prepare($sql);
  5. // 将变量绑定到SQL语句模板的命名占位符上
  6. $cate_id = 4;
  7. $stmt->bindParam('cate_id', $cate_id, PDO::PARAM_INT);
  8. // 执行SQL操作
  9. if ($stmt->execute()) {
  10. if ($stmt->rowCount() > 0) {
  11. echo '成功删除了' . $stmt->rowCount() . '条记录';
  12. }
  13. } else {
  14. die('<pre>' . print_r($stmt->errorInfo(), true));
  15. }
  16. // 销毁pdo, 关闭连接
  17. $pdo = null;

  1. $sql = 'SELECT * FROM `movies` WHERE `cate_id` = :cate_id';
  2. $stmt = $pdo->prepare($sql);
  3. $cate_id = 1;
  4. $stmt->bindParam('cate_id',$cate_id,PDO::PARAM_INT);
  5. //绑定占位符和执行操作顺序改变
  6. $stmt->execute();
  7. while($movie = $stmt->fetch(PDO::FETCH_ASSOC)){
  8. echo '<pre>' . print_r($movie,true);
  9. }
  10. $pdo = null;
  11. //fetchAll()
  12. require 'pdo_config.php';
  13. $sql = 'SELECT * FROM `movies` WHERE `cate_id` = :cate_id';
  14. $stmt = $pdo->prepare($sql);
  15. $cate_id = 3;
  16. $stmt->bindParam('cate_id',$cate_id,PDO::PARAM_INT);
  17. $stmt->execute();
  18. $movies = $stmt ->fetchAll(PDO::FETCH_ASSOC);
  19. foreach($movies as $movie){
  20. echo '<pre>' . print_r($movies,true);
  21. }
  22. unset($pdo);
  23. //bindColumn()
  24. require 'pdo_config.php';
  25. echo '<br>';
  26. $sql = 'SELECT * FROM `movies` WHERE `cate_id` =:cate_id';
  27. $stmt = $pdo->prepare($sql);
  28. $cate_id = 3;
  29. $stmt->bindParam('cate_id',$cate_id,PDO::PARAM_INT);
  30. $stmt->execute();
  31. $stmt->bindColumn('name',$name);
  32. $stmt->bindColumn('detail',$detail);
  33. while ($stmt->fetch(PDO::FETCH_ASSOC)){
  34. $detail = mb_substr($detail,0,20,'utf-8');
  35. echo '片名:' . $name . '<br>' . '简介:' . $detail . '...<hr>';
  36. }
  37. //while是循环,一直到行没有数据为止
  38. $pdo = null;

改:

  1. $sql = 'UPDATE `category` SET `name` = :n, `alias` = :alias WHERE `cate_id`=:cate_id';
  2. $stmt = $pdo->prepare($sql);
  3. $name = 'xjj';
  4. $alias = '小姐姐';
  5. $cate_id = 4;
  6. //确保有这条记录
  7. $stmt->bindParam('n',$name,PDO::PARAM_STR);
  8. //::静态常量,->对外开放的类
  9. $stmt->bindParam('alias', $alias, PDO::PARAM_STR);
  10. $stmt->bindParam('cate_id', $cate_id, PDO::PARAM_INT);
  11. if($stmt->execute()){
  12. if($stmt->rowCount()>0){
  13. echo '成功更新了' . $stmt->rowCount() . '条记录';
  14. }
  15. }else{
  16. die('<pre>' . print_r($stmt->errorInfo(),true));
  17. }
  18. $pdo = null;
  19. print_r($pdo);

Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错,继续加油。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post