Blogger Information
Blog 14
fans 0
comment 1
visits 12859
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用PDO方式增删改查
王珂
Original
863 people have browsed it

使用PDO方式增删改查

数据表user

id name email password status create_time
1 admin admin@php.cn 7c222fb2927d828af22f592134e8932480637c0d 1 1588987748
2 张三 zhangsan@php.cn 7c222fb2927d828af22f592134e8932480637c0d 1 1588987748
3 李四 lisi@php.cn 7c222fb2927d828af22f592134e8932480637c0d 1 1588987748
4 王五 wangwu@php.cn 7c222fb2927d828af22f592134e8932480637c0d 1 1588987748
5 刘六 liuliu@php.cn 7c222fb2927d828af22f592134e8932480637c0d 1 1588987748
6 赵大 zhaoda@php.cn 7c222fb2927d828af22f592134e8932480637c0d 0 1588987748
7 钱二 qianer@php.cn 7c222fb2927d828af22f592134e8932480637c0d 0 1588987748

数据库连接conn.php

  1. <?php
  2. namespace pdo_edu;
  3. use Exception;
  4. use PDO;
  5. //$config = require 'config/database1.php';
  6. $config = [
  7. 'type' => $type ?? 'mysql',
  8. 'host' => $host ?? 'localhost',
  9. 'dbname' => $dbname ?? 'phpedu',
  10. 'username' => $username ?? 'root',
  11. 'password' => $password ?? 'root',
  12. 'charset' => $charset ?? 'utf8',
  13. 'port' => $port ?? '3306',
  14. ];
  15. //print_r($config) ;
  16. $type = $config['type'];
  17. $host = $config['host'];
  18. $dbname = $config['dbname'];
  19. $username = $config['username'];
  20. $password = $config['password'];
  21. $charset = $config['charset'];
  22. $port = $config['port'];
  23. $dsn = sprintf('%s:host=%s;dbname=%s;charset=%s;port=%s',$type,$host,$dbname,$charset,$port);
  24. try{
  25. $pdo = new PDO($dsn,$username,$password);
  26. }catch (Exception $e){
  27. die($e->getMessage());
  28. }

PDO单条查询

  1. <?php
  2. // PDO单条查询
  3. namespace pdo_edu;
  4. use PDO;
  5. //连接数据库
  6. require 'conn.php';
  7. $sql = 'SELECT `id`,`name`,`status` FROM `user` WHERE `status` =1';
  8. //预处理,防止SQL注入
  9. $stmt = $pdo->prepare($sql);
  10. //执行SQL
  11. $stmt->execute();
  12. //使用fetch获取关联数组
  13. while ($user = $stmt->fetch(PDO::FETCH_ASSOC)) {
  14. printf('<pre>%s</pre>',print_r($user,true));
  15. }
  16. //关闭连接
  17. unset($pdo);

结果

Array
(
[id] => 1
[name] => admin
[status] => 1
)
Array
(
[id] => 2
[name] => 张三
[status] => 1
)
Array
(
[id] => 3
[name] => 李四
[status] => 1
)
Array
(
[id] => 4
[name] => 王五
[status] => 1
)
Array
(
[id] => 5
[name] => 刘六
[status] => 1
)

PDO多条查询

  1. <?php
  2. // PDO多条查询
  3. namespace pdo_edu;
  4. use PDO;
  5. //连接数据库
  6. require 'conn.php';
  7. // 匿名占位符: ?
  8. //$sql = 'SELECT `id`,`name`,`status` FROM `user` WHERE `status` = ?';
  9. // 命名占位符: 给一个有意义 的字符串, 必须用冒号开始
  10. $sql = 'SELECT * FROM `user` WHERE `status` = :status';
  11. //预处理,防止SQL注入
  12. $stmt = $pdo->prepare($sql);
  13. //执行SQL
  14. // 对应匿名占位符使用是索引数组
  15. // $stmt->execute([1]);
  16. // 对应命名占位符使用是关联数组
  17. $stmt->execute(['status'=>1]);
  18. //使用fetch_all获取关联数组
  19. $users = $stmt->fetchall(PDO::FETCH_ASSOC);
  20. printf('|id|姓名|状态|创建时间|<br>');
  21. printf('|--|--|--|--|<br>');
  22. foreach ($users as $user) {
  23. $date = date('Y年m月d日', $user['create_time']);
  24. printf('|%s|%s|%s|%s|<br>', $user['id'], $user['name'], $user['status'], $date);
  25. // printf('id=%s:姓名=%s---状态=%s---创建时间=%s<br>', $user['id'], $user['name'], $user['status'], $date);
  26. }
  27. //关闭连接
  28. unset($pdo);

结果

id 姓名 状态 创建时间
1 admin 1 2020年05月09日
2 张三 1 2020年05月09日
3 李四 1 2020年05月09日
4 王五 1 2020年05月09日
5 刘六 1 2020年05月09日

PDO新增记录

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. require 'conn.php';
  5. $sql = 'INSERT `user` SET `name` = ?,`password` = ?,`email`=?,`status` =? ,`create_time` = ?';
  6. $stmt = $pdo->prepare($sql);
  7. $data = ['刘七','7c222fb2927d828af22f592134e8932480637c0d','liuqi@php.cn','1','1588987759'];
  8. $stmt->execute($data);
  9. if ($stmt->rowCount() === 1) {
  10. echo '新增成功, 新增记录的主键是: ' . $pdo->lastInsertId();
  11. } else {
  12. echo '新增失败';
  13. print_r($stmt->errorInfo());
  14. }
  15. // 关闭连接
  16. unset($pdo);

结果

新增成功, 新增记录的主键是: 9

PDO删除记录

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. require 'conn.php';
  5. $sql = 'DELETE FROM `user` WHERE `id`= :id';
  6. $stmt = $pdo->prepare($sql);
  7. $stmt->execute(['id'=>$_GET['id']]);
  8. if ($stmt->rowCount() === 1){
  9. printf('删除成功!');
  10. }
  11. unset($pdo);

结果

删除成功!

PDO更新记录

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. require 'conn.php';
  5. $sql = 'UPDATE `user` SET `name`= ? ,`email` = ? WHERE `id`= ?';
  6. $stmt = $pdo->prepare($sql);
  7. $data = ['王老师','wanglaoshi@php.cn','4'];
  8. $stmt->execute($data);
  9. if ($stmt->rowCount() === 1){
  10. echo "更新成功";
  11. }else{
  12. echo "更新失败";
  13. print_r($stmt->errorInfo());
  14. }
  15. unset($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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!