Blogger Information
Blog 34
fans 0
comment 0
visits 19977
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
常用 的CURD操作
OC的PHP大牛之路
Original
567 people have browsed it

pdo: 预处理

1. 匿名参数 + 索引数组

  1. namespace pdo_edu;
  2. use PDO;
  3. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  4. // 匿名参数: ?
  5. $sql = 'INSERT `staff` SET `name`= ?,`sex`= ?;';
  6. $stmt = $db->prepare($sql);
  7. // 索引数组
  8. $data = ['陈梦', 1];
  9. // 执行sql
  10. $stmt->execute($data);
  11. // 验证: 打印sql预处理命令
  12. echo '新增成功, id = ' . $db->lastInsertId() . '<br>';

2. 命名参数 + 关联数组

  1. namespace pdo_edu;
  2. use PDO;
  3. // 连接
  4. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  5. // 命名参数: ":name"
  6. $sql = 'INSERT `staff` SET `name`= :name,`sex`= :sex;'
  7. $stmt = $db->prepare($sql);
  8. // 关联数组
  9. $data = ['name'=>'孙颖莎', 'sex'=>1];
  10. // 执行sql
  11. $stmt->execute($data);
  12. // 验证: 打印sql预处理命令
  13. echo '新增成功, id = ' . $db->lastInsertId() . '<br>';

3.参数绑定: 引用绑定 bindParam()

  1. namespace pdo_edu;
  2. use PDO;
  3. // 连接
  4. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  5. // CURD: INSERT
  6. $sql = 'INSERT `staff` SET `name`= ?,`sex`= ?;';
  7. $stmt = $db->prepare($sql);
  8. // 引用绑定: 动态绑定,绑定的不是数据本身,而它的地址/引用/别
  9. $stmt->bindParam(1, $name, PDO::PARAM_STR);
  10. $stmt->bindParam(2, $sex, PDO::PARAM_INT);
  11. list($name, $sex, $email)=['王曼昱', 1];
  12. // 执行sql
  13. $stmt->execute();
  14. echo '新增成功, id = ' . $db->lastInsertId() . '<br>';
  1. namespace pdo_edu;
  2. use PDO;
  3. // 连接
  4. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  5. // CURD: INSERT
  6. $sql = 'INSERT `staff` SET `name`= ?,`sex`= ?;';
  7. $stmt = $db->prepare($sql);
  8. // 引用绑定
  9. $stmt->bindParam(1, $name, PDO::PARAM_STR);
  10. $stmt->bindParam(2, $sex, PDO::PARAM_INT);
  11. // 二维数组
  12. $data = [
  13. ['樊振东',0],
  14. ['马龙',0],
  15. ['徐昕',0],
  16. ];
  17. foreach ($data as list($name, $sex)) {
  18. // 执行sql
  19. $stmt->execute();
  20. echo '新增成功, id = ' . $db->lastInsertId() .
  21. '<br>';
  22. }

4.失败检测与处理

  1. namespace pdo_edu;
  2. use PDO;
  3. // 连接
  4. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  5. // CURD: INSERT
  6. $sql = 'INSERT `staff` SET `name`= ?,`sex`= ?;';
  7. $stmt = $db->prepare($sql);
  8. $data = ['刘翔', 0];
  9. // 执行sql
  10. if ($stmt->execute($data)) {
  11. if ($stmt->rowCount() > 0) {
  12. // success
  13. echo '新增成功, id = ' . $db->lastInsertId() .
  14. '<br>';
  15. } else {
  16. // fail
  17. echo '新增失败';
  18. print_r($stmt->errorInfo());
  19. }
  20. } else {
  21. // false
  22. echo 'sql执行失败';
  23. print_r($stmt->errorInfo());
  24. }

5. 更新操作

  1. namespace pdo_edu;
  2. use PDO;
  3. // 连接
  4. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  5. // CURD: UPDATE
  6. $sql = <<< SQL
  7. UPDATE `staff`
  8. SET `name`= ?,`sex`= ?,
  9. WHERE `id` = ? ;
  10. SQL;
  11. if (false === stripos($sql, 'where')) {
  12. exit('禁止无条件更新');
  13. }
  14. $stmt = $db->prepare($sql);
  15. $data = ['苏炳添', 0, 10];
  16. // 执行sql
  17. if ($stmt->execute($data)) {
  18. if ($stmt->rowCount() > 0) {
  19. echo '成功的更新了 '.$stmt->rowCount() . '条记录
  20. ~~';
  21. } else {
  22. echo '没有记录被更新';
  23. print_r($stmt->errorInfo());
  24. }
  25. } else {
  26. echo 'sql执行失败';
  27. print_r($stmt->errorInfo());
  28. }

6.删除操作

  1. namespace pdo_edu;
  2. use PDO;
  3. // 连接
  4. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  5. // CURD: DELETE
  6. $sql = 'DELETE FROM `staff` WHERE `id` = ?;';
  7. if (false === stripos($sql, 'where')) {
  8. exit('禁止无条件删除');
  9. }
  10. $stmt = $db->prepare($sql);
  11. // 执行sql
  12. if ($stmt->execute([9])) {
  13. if ($stmt->rowCount() > 0) {
  14. echo '成功的删除了 '.$stmt->rowCount() . ' 条记录
  15. ~~';
  16. } else {
  17. echo '没有记录被删除';
  18. print_r($stmt->errorInfo());
  19. }
  20. } else {
  21. echo 'sql执行失败';
  22. print_r($stmt->errorInfo());
  23. // $stmt->debugDumpParams();
  24. }

7.查询-1: fetch() + while()

  1. namespace pdo_edu;
  2. use PDO;
  3. // 连接
  4. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  5. // CURD: SELECT
  6. $sql = 'SELECT `id`,`name` FROM `staff`LIMIT ?';
  7. $stmt = $db->prepare($sql);
  8. $stmt->bindValue(1, 3, PDO::PARAM_INT);
  9. // 执行sql
  10. if ($stmt->execute()) {
  11. while ($staff = $stmt->fetch(PDO::FETCH_ASSOC))
  12. {
  13. printf('<pre>%s</pre>', print_r($staff, true));
  14. }
  15. } else {
  16. echo 'sql执行失败';
  17. print_r($stmt->errorInfo());
  18. }
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