Blogger Information
Blog 24
fans 0
comment 0
visits 18496
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO操作数据库
昔年
Original
683 people have browsed it

PDO 操作数据库

1.创建数据表

  1. drop table if exists 'category';
  2. create table 'category' (
  3. 'id' int(10) unsigned not null AUTO_INCREMENT,
  4. 'name' varchar(16) COLLATE utf8mb4_unicode_ci not null,
  5. 'cate_order' int(10) not null,
  6. 'description' varchar(100) COLLATE utf8mb4_unicode_ci not null,
  7. 'parent_id' int(10) not null,
  8. 'created_at' timestamp null DEFAULT null,
  9. 'updated_at' timestamp null DEFAULT null,
  10. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

2.连接数据库

2.1数据库配置文件

  1. <?php
  2. namespace pdo_edu;
  3. return [
  4. 'type' => $type ?? 'mysql',
  5. 'host' => $host ?? 'localhost',
  6. 'chartset' => $charset ?? 'utf8',
  7. 'dbname' => $dbname ?? 'phpedu',
  8. 'username' => $username ?? 'root',
  9. 'password' => $pasword ?? 'root',
  10. 'port' => $port ?? '3306'
  11. ];

2.1使用pdo链接数据

  1. <?php
  2. namespace pdo_edu;
  3. use Exception;
  4. use PDO;
  5. $config = require 'config/database.php';
  6. extract($config);
  7. //创建DSN
  8. $dsn = sprintf("%s:host=%s;dbname=%s", $type, $host, $dbname);
  9. // var_dump($dsn);
  10. try {
  11. //链接数据库
  12. $pdo = new PDO($dsn, $username, $password);
  13. $pdo->query("set names utf8");
  14. } catch (Exception $e) {
  15. die($e->getMessage());
  16. }

3.使用PDO实现对数据库的读操作

  1. <?php
  2. namespace select;
  3. use PDO;
  4. header("Content-Type: text/html; charset=UTF-8");
  5. //1.链接数据库
  6. require 'connect.php';
  7. //2.操作数据表
  8. $sql = "select * from `category` where `parent_id` = ?";
  9. // 预处理对象$stmt:为了防止 SQL注入
  10. $stmt = $pdo->prepare($sql);
  11. $stmt->execute([1]);
  12. // $categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
  13. // var_dump($categories);
  14. while ($category = $stmt->fetch(PDO::FETCH_ASSOC)) {
  15. printf("<pre>%s</pre>", print_r($category, true));
  16. }
  17. //3.关闭链接
  18. unset($pdo);

4.使用PDO实现对数据的增操作

  1. <?php
  2. namespace insert;
  3. use PDO;
  4. //1.链接数据库
  5. require 'connect.php';
  6. //2.操作数据表
  7. $sql = "insert `category` set `name`=?, `cate_order`=?, `description`=?, `parent_id`=?, `created_at`=?, `updated_at`=? ";
  8. // echo $sql;
  9. // exit;
  10. $stmt = $pdo->prepare($sql);
  11. $data = [
  12. '网易新闻', '6', '网易新闻是网易倾力打造的精品应用,已然成为国内第一新闻客户端,因体验最流畅、新闻最快速、评论最犀利而备受推崇',
  13. '1', '2020-05-08 12:00:00', '2020-05-08 12:18:12'
  14. ];
  15. $stmt->execute($data);
  16. //判断是否执行成功
  17. if ($stmt->rowCount() === 1) {
  18. echo '新增成功,新增的主键是' . $pdo->lastInsertId();
  19. } else {
  20. echo '新增失败';
  21. print_r($stmt->errorInfo());
  22. }
  23. //3.关闭链接
  24. unset($pdo);

5.使用PDO实现对数据的改操作

  1. <?php
  2. namespace update;
  3. use PDO;
  4. //1.链接数据库
  5. require 'connect.php';
  6. //2.数据库操作
  7. $sql = "update `category` set `description`=? where `id` =? ";
  8. $stmt = $pdo->prepare($sql);
  9. $data = ['网易新闻融合资讯平台及原创策划为一体,自1998年成立起始终保持市场领先地位。', 12];
  10. $stmt->execute($data);
  11. //判断是否执行成功
  12. if ($stmt->rowCount() === 1) {
  13. echo '更新成功';
  14. } else {
  15. echo '更新失败';
  16. print_r($pdo->errorInfo());
  17. }
  18. //3.关闭链接
  19. unset($pdo);

6.使用PDO实现对数据库的删操作

  1. <?php
  2. namespace delete;
  3. use PDO;
  4. //1.建立链接
  5. require "connect.php";
  6. //2.数据库操作
  7. $sql = "delete from `category` where `id`=?";
  8. // $sql = "DELETE FROM `staffs` WHERE `id`=:id";
  9. // echo $sql;
  10. $stmt = $pdo->prepare($sql);
  11. $stmt->execute(['13']);
  12. if ($stmt->rowCount() === 1) {
  13. echo '删除成功';
  14. } else {
  15. echo '删除失败';
  16. print_r($pdo->errorInfo());
  17. }
  18. //3.关闭链接
  19. unset($pdo);

总结:使用PDO操作数据库主要有3个步骤,第一步是链接数据库,由于可能经常需要使用到这个操作,可以把它单独拿出来写在一个文件中,后面需要连接数据库时引入这个文件就行;第二歩对数据库进行操作,主要先写SQL语句然后使用prepare方法对SQL语句进行预处理最后执行execute方法传入相应的参数;第三歩是可选的,关闭连接,直接用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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!