Blogger Information
Blog 13
fans 1
comment 0
visits 14935
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库入门及PDO的增删改
樊天龙的博客
Original
948 people have browsed it

数据库入门及PDO的增删改

1.数据库介绍

  • MySQL是一款开源免费的关系型数据库软件,适合中小型项目
  • 增删改查(CURD)是数据库的招牌动作
  • 可视化数据库管理软件有Navicat,PHPMyadmin,adminer等

2.PDO介绍

  • PDO是PHP的一个拓展,是数据库抽象层,适合绝大多数数据库
  • 简单来说,PDO操作数据库的代码是一样的,而不用关心是什么数据库,只要更改DSN即可
  • 需要在php.ini中开启PDO拓展
  • 早期还有mysql拓展,mysqli拓展,仅仅支持mysql数据库而已

3.DDL创建数据库,数据表和数据库配置连接文件

  1. CREATE DATABASE IF NOT EXISTS `test` CHARSET UTF8 COLLATE UTF8_GENERAL_CI;
  2. CREATE TABLE IF NOT EXISTS `customers`
  3. (
  4. `cust_id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  5. `cust_name` CHAR(50) NOT NULL,
  6. `cust_sex` CHAR(1) NOT NULL DEFAULT 0,
  7. `cust_address` CHAR(50),
  8. `cust_contact` CHAR(50)
  9. ) ENGINE INNODB
  10. CHARSET UTF8
  11. COLLATE UTF8_GENERAL_CI;
  1. <?php
  2. namespace config;
  3. return [
  4. //数据库类型
  5. 'type' => $type ?? 'mysql',//相当于isset($type)?$type:'localhost'
  6. //主机
  7. 'host' => $host ?? 'localhost',
  8. //默认数据库
  9. 'dbname' => $dbname ?? 'php11',
  10. //用户名
  11. 'username' => $username ?? 'root',
  12. //密码
  13. 'password' => $password ?? 'root',
  14. //端口
  15. 'port' => $port ?? 3306,
  16. //默认字符集
  17. 'charset' => $charset ?? 'UTF8'
  18. ];
  1. <?php
  2. namespace connect;
  3. use PDO;
  4. use Exception;
  5. //引入数据库配置文件
  6. $config = require_once './config/config.php';
  7. //获取数据库配置
  8. $type = $config['type'];
  9. $host = $config['host'];
  10. $dbname = $config['dbname'];
  11. $username = $config['username'];
  12. $password = $config['password'];
  13. //数据源
  14. $dsn = $type . ':' . 'host=' . $host . ';dbname=' . $dbname;
  15. //连接数据库
  16. try {
  17. $pdo = new PDO($dsn, $username, $password);
  18. echo '数据库连接成功<br>';
  19. } catch (Exception $e) {
  20. echo $e->getMessage();
  21. }

4.PDO增加数据

  1. <?php
  2. namespace delete;
  3. //引入数据库连接文件
  4. require_once './connect.php';
  5. //SQL
  6. $sql = "INSERT INTO `customers` VALUES (null, ?, ?, ?, ?);";
  7. //预处理语句
  8. $stmt = $pdo->prepare($sql);
  9. //数据
  10. $data = ['张三', 0, '黑龙江省哈尔滨市', 18888888888];
  11. //参数绑定(按位置进行依次绑定),并执行SQL
  12. $stmt->execute($data);
  13. //是否执行成功
  14. if ($stmt->rowCount() > 0) {
  15. echo '数据新增成功,最后一条插入的ID为:' . $pdo->lastInsertId();
  16. } else {
  17. echo '数据新增失败,错误信息为:';
  18. print_r($stmt->errorInfo());
  19. }

5.PDO删除数据

  1. <?php
  2. namespace delete;
  3. //引入数据库连接文件
  4. require_once './connect.php';
  5. //SQL
  6. $sql = "DELETE FROM `customers` WHERE `cust_id` = :cust_id;";
  7. //预处理语句
  8. $stmt = $pdo->prepare($sql);
  9. //数据
  10. $data = [':cust_id'=>1];
  11. //参数绑定(按名称进行绑定),并执行SQL
  12. $stmt->execute($data);
  13. //是否执行成功
  14. if ($stmt->rowCount() > 0) {
  15. echo '数据删除成功';
  16. } else {
  17. echo '数据删除失败,错误信息为:';
  18. print_r($stmt->errorInfo());
  19. }

6.PDO更新数据

  1. <?php
  2. namespace delete;
  3. //引入数据库连接文件
  4. require_once './connect.php';
  5. //SQL
  6. $sql = "UPDATE `customers` SET `cust_address` = :cust_address WHERE `cust_id` = :cust_id;";
  7. //预处理语句
  8. $stmt = $pdo->prepare($sql);
  9. //数据
  10. $data = [
  11. ':cust_address' => '北京市',
  12. ':cust_id' => 6
  13. ];
  14. //参数绑定(按名称进行绑定),并执行SQL
  15. $stmt->execute($data);
  16. //是否执行成功
  17. if ($stmt->rowCount() > 0) {
  18. echo '数据更新成功';
  19. } else {
  20. echo '数据更新失败,错误信息为:';
  21. print_r($stmt->errorInfo());
  22. }

7.总结

  • PDO是PHP操纵数据库的重要拓展,必须学会
  • 相比较其他PHP操作数据库的拓展更加有发展
  • 无论是原生开发,或者框架开发都是PDO,框架底层为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