Blogger Information
Blog 29
fans 0
comment 0
visits 14062
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库学习小结
P粉317509817
Original
546 people have browsed it

链接数据库

database.php

  1. <?php
  2. namespace pdo_edu;
  3. return [
  4. // dsn信息
  5. 'type'=>'mysql',
  6. 'host'=>'127.0.0.1',
  7. 'dbname'=>'phpedu',
  8. 'port'=>'3306',
  9. 'charset'=>'utf8',
  10. 'username'=>'root',
  11. 'password'=>'root',
  12. ];

connect.php

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. $dbConfig = require 'database.php';
  5. extract($dbConfig);//将$dbConfig里面的key和value转换为变量和值
  6. // 1.dsn
  7. $tpl='%s:host=%s;dbname=%s;port=%s;charset=%s';//输出模板
  8. $args = [$type,$host,$dbname,$port,$charset];
  9. $dsn = sprintf($tpl,...$args);//返回输出模板+数组一一对应的字符串
  10. // 2.创建数据对象
  11. $db = new PDO($dsn,$username,$password);
  12. // 设置结果机的默认获取方式:只要关联部分
  13. $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);

demo1.php

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. // 链接数据库
  5. require __DIR__ . 'config/connect.php';

至此,数据库链接成功

CURD操作

INSERT操作

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. // 链接数据库
  5. // echo __DIR__;
  6. require __DIR__ . '/config/connect.php';
  7. // INSERT操作
  8. // INSERT 表名 SET 字段1=值1, 字段2=值2, ....
  9. // 预处理模板
  10. $sql = 'INSERT `people` SET `name`=?,`gender`=?,`email`=?';
  11. // 创建SQL语句模板对象
  12. $stmt = $db->prepare($sql);
  13. // 执行SQL语句
  14. $stmt->execute(['aaa',1,'aaaa@qq.com']);
  15. $stmt->execute(['bbb',1,'aaaa@qq.com']);
  16. $stmt->execute(['ccc',1,'aaaa@qq.com']);
  17. $stmt->execute(['ddd',1,'aaaa@qq.com']);
  18. $stmt->execute(['eee',1,'aaaa@qq.com']);
  19. $stmt->execute(['fff',1,'aaaa@qq.com']);
  20. $stmt->execute(['ggg',1,'aaaa@qq.com']);
  21. $stmt->execute(['hhh',1,'aaaa@qq.com']);
  22. $stmt->execute(['iii',1,'aaaa@qq.com']);
  23. $stmt->execute(['jjj',1,'aaaa@qq.com']);
  24. // 校验是否添加成功
  25. // $stmt->rowCount(): 返回受影响的记录数量
  26. if($stmt->rowCount()>0){
  27. echo'新增成功,新增记录的主键ID为:'.$db->lastInsertId();
  28. }else{
  29. echo'新增失败';
  30. print_r($stmt->errorInfo());
  31. }

效果:

UPDATE操作

  1. // UPDATE操作
  2. $sql1='UPDATE `people` SET `gender` = ? WHERE `id`=?';
  3. $stmt = $db->prepare($sql1);
  4. $stmt->execute([0,21]);
  5. if ($stmt->rowCount()>0){
  6. echo '更新成功';
  7. }else{
  8. echo '更新失败';
  9. print_r($stmt->errorInfo());
  10. }

效果:

DELETE操作

  1. $sql1 = 'DELETE FROM `people` WHERE `id`=?';
  2. $stmt=$db->prepare($sql1);
  3. $stmt->execute([30]);
  4. if ($stmt->rowCount() > 0) {
  5. echo ' 删除成功';
  6. } else {
  7. echo '删除失败';
  8. print_r($stmt->errorInfo());
  9. }

效果:

SELECT操作

单条查询

  1. // SELECT单条查询
  2. $sql = 'SELECT `id`,`name` FROM `people` WHERE `id` > ?';
  3. $stmt = $db->prepare($sql);
  4. $stmt->execute([25]);
  5. // PDO::FETCH_ASSOC: 结果集获取模式,只返回关联部分
  6. while ($staff = $stmt->fetch()) {
  7. printf('<pre>%s</pre>', print_r($staff, true));
  8. }

效果:

多条查询`

  1. $sql = 'SELECT `id`,`name` FROM `people` WHERE `id` > ?';
  2. $stmt = $db->prepare($sql);
  3. $stmt->execute([10]);
  4. // fetchAll: 返回全部满足条件的记录集合,二维数组
  5. $staffs = $stmt->fetchAll();
  6. // print_r($staffs);
  7. foreach ($staffs as $staff) {
  8. printf('<pre>%s</pre>', print_r($staff, true));
  9. }

效果;

PDO的本质和原理

PDO本质:

PDO(PHP Data Objects)是一种在PHP里连接数据库的使用接口。PDO与mysqli曾经被建议用来取代原本PHP在用的mysql相关函数,基于数据库使用的安全性,因为后者欠缺对于SQL注入的防护。

PDO预处理

  • 为什么要用预处理?
    1. 防止SQL注入攻击,
    1. 数据延迟绑定
  • (编程时只写SQL语句模板,执行SQL时再给占位符绑定真实数据)
  • 预处理过程:
    1. 创建SQL语句模板对象: 数据使用占位符表示
    1. 执行SQL语句,根据操作类型(写/读),读返回结果集/数组, 写返回受影响的记录数量
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