Blogger Information
Blog 62
fans 3
comment 1
visits 29550
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO的CURD操作
kiraseo_wwwkiraercom
Original
291 people have browsed it

PDO的CURD操作

增加操作

代码

  1. <?php
  2. // 实例演示常用 的CURD操作,特别是各种常用组合,如fetch+while..
  3. namespace pdo_edu;
  4. use PDO;
  5. // 连接
  6. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  7. //执行添加操作
  8. // CURD: INSERT
  9. $sql = 'INSERT `user` SET `name`= ?,`age`= ?,`email`= ?;';
  10. $res = $db->prepare($sql);
  11. // 引用绑定
  12. $res ->bindParam(1, $name, PDO::PARAM_STR);
  13. $res ->bindParam(2, $age, PDO::PARAM_INT);
  14. $res ->bindParam(3, $email, PDO::PARAM_STR);
  15. // 准备多条数据: 二维数组来模拟
  16. $data = [
  17. ['草莓',18,'strawberry@qq.com'],
  18. ['菠萝',18,'pineapple@qq.com'],
  19. ['芒果',18,'Mango@qq.com'],
  20. ['杏',18,'apricot@qq.com'],
  21. ['李子',18,'plum@qq.com'],
  22. ['西瓜',18,'watermelon@qq.com'],
  23. ['木瓜',18,'Papaya@qq.com'],
  24. ['哈密瓜',18,'Hami melon@qq.com'],
  25. ['山竹',18,'Mangosteen@qq.com'],
  26. ['樱桃',18,'Cherry@qq.com'],
  27. ];
  28. foreach ($data as list($name, $age, $email)) {
  29. // 执行sql
  30. $stmt->execute();
  31. echo '新增成功, id = ' . $db->lastInsertId() . '<br>';
  32. }

演示效果

更新操作

代码

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  6. //执行修改操作
  7. // CURD: UPDATE
  8. $sql = 'UPDATE `user` SET `name`= ?,`age`= ?,`email`= ? where id=?;';
  9. //检查sql是否满足修改条件
  10. if (false === stripos($sql, 'where')) {
  11. exit('无条件下禁止更新,请检查');
  12. }
  13. $res = $db->prepare($sql);
  14. $data = ['杏干','19','xinggan@qq.com',4];
  15. // 执行sql语句
  16. if($res->execute($data)){
  17. if($res->rowCount()>0){
  18. echo '成功的更新'.$res->rowCount().'条记录';
  19. }else{
  20. echo '更新失败';
  21. }
  22. }else{
  23. echo 'sql发生错误';
  24. }

演示效果

删除操作

代码

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  6. //执行删除操作
  7. // CURD: DELETE
  8. $sql = 'DELETE FROM `user` where id=?;';
  9. //检查sql是否满足修改条件
  10. if (false === stripos($sql, 'where')) {
  11. exit('无条件下禁止更新,请检查');
  12. }
  13. $res = $db->prepare($sql);
  14. $data = [10];
  15. // 执行sql语句
  16. if($res->execute($data)){
  17. if($res->rowCount()>0){
  18. echo '删除'.$res->rowCount().'条记录';
  19. }else{
  20. echo '删除失败';
  21. }
  22. }else{
  23. echo 'sql发生错误';
  24. }

演示效果

查询操作

代码

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  6. //执行查询操作
  7. // CURD: DELETE
  8. $sql = 'SELECT id,name,age FROM `user` limit ?;';
  9. $res = $db->prepare($sql);
  10. $num = 6;
  11. $res->bindValue(1,$num, PDO::PARAM_INT);
  12. // 执行sql语句
  13. if($res->execute()){
  14. $users = $res->fetchAll(PDO::FETCH_ASSOC);
  15. foreach($users as $user){
  16. echo "<pre>";
  17. var_dump($user);
  18. echo "</pre>";
  19. }
  20. }else{
  21. echo 'sql发生错误';
  22. var_dump($res->errorInfo());
  23. }

演示效果

Correcting teacher:PHPzPHPz

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