Blogger Information
Blog 34
fans 2
comment 0
visits 23169
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月22号作业 PDO操作数据库,增删查改
遗忘了寂寞
Original
751 people have browsed it

PDO连接数据库

  1. <?php
  2. // 数据库连接参数
  3. $db = [
  4. 'type' => 'mysql',
  5. 'host' => 'localhost',
  6. 'dbname' => 'zy2019_11_21',
  7. 'username' => 'root',
  8. 'password' => '123123',
  9. ];
  10. // 配置数据源DSN信息
  11. $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
  12. // 连接数据库
  13. try {
  14. $pdo = new PDO($dsn, $db['username'], $db['password']);
  15. } catch (PDOException $e) {
  16. die('Connection Failed: ' . $e->getMessage());
  17. }
  18. ?>

查询数据

  1. <?php
  2. include 'conn.php';
  3. echo '查询1: fetch() + while()';
  4. // 1. 创建SQL语句模板
  5. $sql = 'SELECT * FROM `goods` WHERE `gdtypeid` = :gdtypeid';
  6. // 2. 创建SQL语句对象
  7. $stmt = $pdo->prepare($sql);
  8. // 3. 将变量绑定到SQL语句模板的命名占位符上
  9. $gdtypeid = 2;
  10. $stmt->bindParam('gdtypeid', $gdtypeid, PDO::PARAM_INT);
  11. // 4. 执行SQL操作
  12. $stmt->execute();
  13. // 遍历
  14. while ($goods = $stmt->fetch(PDO::FETCH_ASSOC)) {
  15. echo '<pre>' . print_r($goods, true);
  16. }
  17. echo '<hr>';
  18. echo '查询2: fetchAll()';
  19. // 创建SQL语句模板
  20. $sql = 'SELECT * FROM `gdtype`' ;
  21. // 创建SQL语句对象
  22. $stmt = $pdo->prepare($sql);
  23. // 5. 执行SQL操作
  24. $stmt->execute();
  25. // 遍历, 返回二维数组
  26. $gdtype = $stmt->fetchAll(PDO::FETCH_ASSOC);
  27. foreach ($gdtype as $gdtype) {
  28. echo '<pre>' . print_r($gdtype, true);
  29. }
  30. // 销毁pdo, 关闭连接
  31. $pdo = null;
  32. ?>

插入数据

  1. <?php
  2. include 'conn.php';
  3. echo '插入数据' .'<hr>';
  4. // 1. 创建SQL语句模板
  5. // $sql = 'INSERT INTO `goods` SET `code`= :code, `name`=:name, `gdtypeid`=:gdtypeid, `unit`=:unit, `sprice`=:sprice, `price`=:price';
  6. $sql = 'INSERT INTO `goods` (`code`,`name`,`gdtypeid`,`unit`,`sprice`,`price`) VALUES (:code,:name,:gdtypeid,:unit,:sprice,:price)';
  7. // 2. 创建SQL语句对象
  8. $stmt = $pdo->prepare($sql);
  9. // 3. 将变量绑定到SQL语句模板的命名占位符上
  10. $code = 'W001';
  11. $stmt->bindParam('code', $code, PDO::PARAM_STR);
  12. $name = '商品名称1';
  13. $stmt->bindParam('name', $name, PDO::PARAM_STR);
  14. $gdtypeid = 4;
  15. $stmt->bindParam('gdtypeid', $gdtypeid, PDO::PARAM_INT);
  16. $unit = '个';
  17. $stmt->bindParam('unit', $unit, PDO::PARAM_STR);
  18. $sprice = 10.50;
  19. $stmt->bindParam('sprice', $sprice, PDO::PARAM_STR);
  20. $price = 5.50;
  21. $stmt->bindParam('price', $price, PDO::PARAM_STR);
  22. if ($stmt->execute()) {
  23. if ($stmt->rowCount() > 0) {
  24. echo '成功添加' . $stmt->rowCount(). '条记录, 该记录的主键id是: ' . $pdo->lastInsertId();
  25. }
  26. } else {
  27. die('<pre>' . print_r($stmt->errorInfo(), true));
  28. }
  29. // 销毁pdo, 关闭连接
  30. $pdo = null;
  31. ?>


更新数据

  1. <?php
  2. include 'conn.php';
  3. echo '更新数据' .'<hr>';
  4. // 1. 创建SQL语句模板
  5. $sql = 'UPDATE `goods` SET `name` = :name WHERE `goodsid`=:goodsid';
  6. // 2. 创建SQL语句对象
  7. $stmt = $pdo->prepare($sql);
  8. // 3. 将变量绑定到SQL语句模板的命名占位符上
  9. $goodsid =32;
  10. $stmt->bindParam('goodsid', $goodsid, PDO::PARAM_INT);
  11. $name = '商品名称修改';
  12. $stmt->bindParam('name', $name, PDO::PARAM_STR);
  13. if ($stmt->execute()) {
  14. if ($stmt->rowCount() > 0) {
  15. echo '成功修改' . $stmt->rowCount(). '条记录';
  16. }
  17. } else {
  18. die('<pre>' . print_r($stmt->errorInfo(), true));
  19. }
  20. // 销毁pdo, 关闭连接
  21. $pdo = null;
  22. ?>


删除数据

  1. <?php
  2. include 'conn.php';
  3. echo '删除数据' .'<hr>';
  4. // 1. 创建SQL语句模板
  5. $sql = 'DELETE FROM `goods` WHERE `goodsid`=:goodsid';
  6. // 2. 创建SQL语句对象
  7. $stmt = $pdo->prepare($sql);
  8. // 3. 将变量绑定到SQL语句模板的命名占位符上
  9. $goodsid =31;
  10. $stmt->bindParam('goodsid', $goodsid, PDO::PARAM_INT);
  11. if ($stmt->execute()) {
  12. if ($stmt->rowCount() > 0) {
  13. echo '成功删除' . $stmt->rowCount(). '条记录';
  14. }
  15. } else {
  16. die('<pre>' . print_r($stmt->errorInfo(), true));
  17. }
  18. // 销毁pdo, 关闭连接
  19. $pdo = null;
  20. ?>


手写代码






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