Blogger Information
Blog 26
fans 0
comment 0
visits 18144
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO操作数据库的CURD
雪~人胖胖
Original
695 people have browsed it

1.数据库的连接

1.1连接参数

  1. namespace pdo_edu;
  2. return [
  3. //1.数据库类型
  4. 'type'=>$type ?? 'mysql',
  5. //2.数据库默认主机
  6. 'host'=>$host ?? 'localhost',
  7. //3.默认数据库
  8. 'dbname'=>$dbname ?? 'phpedu' ,
  9. //4.默认编码集
  10. 'charset'=>$charset ?? 'utf8',
  11. 'port'=>$port ?? '3306',
  12. //5.默认用户名
  13. 'username'=>$username ?? 'root',
  14. //6.默认密码
  15. 'password'=>$password ?? 'root',
  16. ];

1.2用pdo连接数据库

  1. //设置命名空间
  2. namespace pdo_edu;
  3. use Exception;
  4. use PDO;
  5. //PDO连接数据库 加载参数
  6. $config = require 'config/database.php';
  7. //PDO连接数据库的三大要素
  8. //数据源:DSN:数据库类型:host=数据库主机地址;dbname=默认的数据库名称;chart=...;port=...
  9. //用户名:username
  10. //用户密码:password
  11. //拿到数据库连接的内容
  12. $type = $config['type'];
  13. $host = $config['host'];
  14. $dbname = $config['dbname'];
  15. $username = $config['username'];
  16. $password = $config['password'];
  17. //组装dsn
  18. $dsn = sprintf('%s:host=%s;dbname=%s',$type,$host,$dbname);
  19. //连接通常放到try中
  20. try
  21. { //连接数据库
  22. $pdo = new PDO($dsn,$username,$password);
  23. } catch(Exception $e)
  24. {
  25. die($e->getMessage());
  26. }

2.数据库的查询操作

  1. namespace pdo_edu;
  2. use PDO;
  3. //1.连接数据库
  4. require 'connect.php';
  5. //2.操作数据表
  6. // $sql = 'SELECT 字段列表 FROM 数据表名称 WHERE 查询条件'
  7. $sql = 'SELECT * FROM `staffs` WHERE `sex`=1';
  8. //预处理对象,防止sql的注入
  9. $stmt = $pdo->prepare($sql);
  10. //使用预处理对象调用execute()执行sql语句
  11. $stmt->execute();
  12. //使用debugDumpParams()调试/查看
  13. // var_dump($stmt->debugDumpParams());
  14. //获取表中的一条记录
  15. // $staff = $stmt->fetch(PDO::FETCH_ASSOC);
  16. //查询全部
  17. while($staff = $stmt->fetch(PDO::FETCH_ASSOC))
  18. {
  19. printf('<pre>%s</pre>',print_r($staff,true));
  20. }
  21. //多条查询
  22. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  23. //结果是一个二维数组用foreach遍历
  24. //3.关闭连接
  25. unset($pdo);

3.数据库新增操作

  1. namespace pdo_edu;
  2. use PDO;
  3. require 'connect.php';
  4. //新增
  5. // $sql = 'INSERT 表名 SET 字段...';
  6. $sql = 'INSERT `staffs` SET `name`=?,`sex`=?,`phone`=?';
  7. $stmt = $pdo->prepare($sql);
  8. $data = ['Tim',1,15656565656];
  9. $stmt->execute($data);
  10. //判断是否执行成功
  11. //$stmt->rowCount():返回写操作产生的受影响的记录数量
  12. if ($stmt->rowCount()===1){
  13. echo '新增成功,新增的主键是'.$pdo->lastInsertId();
  14. }
  15. unset($pdo);

4.数据库更新操作

  1. namespace pdo_edu;
  2. use PDO;
  3. require 'connect.php';
  4. // $sql = 'UPDATE 表名 SET 字段=新值 WHERE 更新条件 '
  5. $sql = "UPDATE `staffs` SET `name`=? WHERE `id`=?";
  6. $stmt = $pdo->prepare($sql);
  7. $stmt->execute(['王老师',4]);
  8. if ($stmt->rowCount()===1){
  9. echo '更新成功';
  10. }else{
  11. echo'无记录被修改哦';
  12. print_r($stmt->errorInfo());
  13. }
  14. unset($pdo);

5.数据库删除操作

  1. namespace pdo_edu;
  2. use PDO;
  3. require 'connect.php';
  4. // $sql = 'DELETE FROM 表名 WHERE 删除条件'
  5. $sql = 'DELETE FROM `staffs` WHERE `id`=:id';
  6. $stmt = $pdo->prepare($sql);
  7. $id = filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT);
  8. $stmt->execute(['id'=>$_GET['id']]);
  9. if ($stmt->rowCount()===1){
  10. echo '删除成功';
  11. }else{
  12. echo '删除失败';
  13. }
  14. unset($pdo);

总结

1.pdo操作数据库的步骤:连接数据库,操作数据库,关闭连接
2.增删改查insert,delete,update,select
3.预处理对象,防止sql的注入 $stmt = $pdo->prepare($sql)
4.使用预处理对象调用execute()执行sql语句 $stmt->execute();

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:sql语句绝对不止咱们课上讲得这些, 将全部语句讲一遍,即没必要也不现实, 所以, 课后还要多看看手册, 了解一些其它语句的功能
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!