Blogger Information
Blog 36
fans 1
comment 0
visits 26409
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示PDO增删改查操作
早晨
Original
576 people have browsed it

匿名参数+索引数组

  1. <?php
  2. namespace pdo;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
  6. // 匿名参数+索引数组
  7. $sql = 'INSERT `staff` SET `name`= ?,`sex`= ?,`email`= ?;';
  8. $stmt = $db->prepare($sql);
  9. $data = ['小明', 1, 'abc@qq.com'];
  10. // 执行
  11. $stmt->execute($data);
  12. echo "匿名参数+索引数组:新增了" . $stmt->rowCount() . "条记录";

运行效果

命名参数+关联数组

  1. <?php
  2. namespace pdo;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
  6. // 命名参数+关联数组
  7. $sql = 'INSERT `staff` SET `name`= :name,`sex`= :sex,`email`= :email;';
  8. $stmt = $db->prepare($sql);
  9. $data = ['name' => '小红', 'sex' => 0, 'email' => 'bcd@qq.com'];
  10. // 执行
  11. $stmt->execute($data);
  12. echo "命名参数+关联数组:新增了" . $stmt->rowCount() . "条记录";

运行效果

参数绑定—值绑定

  1. <?php
  2. namespace pdo;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
  6. // 参数绑定--值绑定
  7. $sql = 'INSERT `staff` SET `name`= ?,`sex`=?,`email`= ?;';
  8. $stmt = $db->prepare($sql);
  9. list($name, $sex, $email) = ['小丽', 1, 'cba@qq.com'];
  10. $stmt->bindValue(1, $name, PDO::PARAM_STR);
  11. $stmt->bindValue(2, $sex, PDO::PARAM_INT);
  12. $stmt->bindValue(3, $email, PDO::PARAM_STR);
  13. // 执行
  14. $stmt->execute();
  15. echo "参数绑定--值绑定: 新增了" . $stmt->rowCount() . "条记录";

运行效果

参数绑定—引用绑定

  1. <?php
  2. namespace pdo;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
  6. // 参数绑定--引用绑定
  7. $sql = 'INSERT `staff` SET `name`= ?,`sex`=?,`email`= ?;';
  8. $stmt = $db->prepare($sql);
  9. $stmt->bindParam(1, $name, PDO::PARAM_STR);
  10. $stmt->bindParam(2, $sex, PDO::PARAM_INT);
  11. $stmt->bindParam(3, $email, PDO::PARAM_STR);
  12. list($name, $sex, $email) = ['小蒙', 0, 'dbc@qq.com'];
  13. // 执行
  14. $stmt->execute();
  15. echo "参数绑定--引用绑定: 新增了" . $stmt->rowCount() . "条记录";

运行效果

一次性增加多条

  1. <?php
  2. namespace pdo;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
  6. // 一次性增加多条
  7. $sql = 'INSERT `staff` SET `name`= ?,`sex`=?,`email`= ?;';
  8. $stmt = $db->prepare($sql);
  9. $stmt->bindParam(1, $name, PDO::PARAM_STR);
  10. $stmt->bindParam(2, $sex, PDO::PARAM_INT);
  11. $stmt->bindParam(3, $email, PDO::PARAM_STR);
  12. $data = [
  13. ['小王', 0, 'fbd@qq.com'],
  14. ['小五', 1, 'sbb@qq.com'],
  15. ['小平', 1, 'ebq@qq.com'],
  16. ['小蓝', 0, 'rbk@qq.com'],
  17. ];
  18. // 执行
  19. foreach ($data as list($name, $sex, $email)) {
  20. $stmt->execute();
  21. echo "一次性增加多条: 新增了" . $stmt->rowCount() . "条记录<br>";
  22. }

运行效果

数据更新

  1. <?php
  2. namespace pdo;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
  6. // 更新
  7. $sql = 'UPDATE `staff` SET `name`= ?,`sex`= ?,`email`= ? WHERE `id`=?';
  8. if (false === stripos($sql, 'where')) {
  9. exit('禁止无条件更新');
  10. }
  11. $stmt = $db->prepare($sql);
  12. $data = ['小猪', 0, 'obd@qq.com', 5];
  13. // 执行
  14. if ($stmt->execute($data)) {
  15. if ($stmt->rowCount() > 0) {
  16. echo "成功更新了" . $stmt->rowCount() . "条数据!";
  17. } else {
  18. echo "没有记录被更新";
  19. }
  20. } else {
  21. echo "执行失败";
  22. }

运行效果

数据删除

  1. <?php
  2. namespace pdo;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
  6. // 删除
  7. $sql = 'DELETE FROM `staff` WHERE `id`=?';
  8. if (false === stripos($sql, 'where')) {
  9. exit('禁止无条件更新');
  10. }
  11. $stmt = $db->prepare($sql);
  12. // $data = [4];
  13. // 执行
  14. if ($stmt->execute([4])) {
  15. if ($stmt->rowCount() > 0) {
  16. echo "成功删除了" . $stmt->rowCount() . "条数据!";
  17. } else {
  18. echo "没有删除数据";
  19. }
  20. } else {
  21. echo "执行失败";
  22. }

运行效果

while和foreach查询

  1. <?php
  2. namespace pdo;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'gommy', '4889671');
  6. // 查询
  7. $sql = 'SELECT * FROM `staff` LIMIT ?';
  8. $stmt = $db->prepare($sql);
  9. $stmt->bindValue(1, 2, PDO::PARAM_INT);
  10. // 执行
  11. echo "<br>-----------------------------while查询-----------------------------";
  12. if ($stmt->execute()) {
  13. while ($sta = $stmt->fetch(PDO::FETCH_ASSOC)) {
  14. printf('<pre>%s</pre>', print_r($sta, true));
  15. }
  16. } else {
  17. echo "执行失败";
  18. }
  19. echo "<br>-----------------------------foreach查询-----------------------------";
  20. $stmt->bindValue(1, 3, PDO::PARAM_INT);
  21. if ($stmt->execute()) {
  22. $sta_all = $stmt->fetchAll(PDO::FETCH_ASSOC);
  23. foreach ($sta_all as $sta_a) {
  24. printf('<pre>%s</pre>', print_r($sta_a, true));
  25. }
  26. } else {
  27. echo "执行失败";
  28. }

运行效果

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!