Blogger Information
Blog 53
fans 3
comment 0
visits 55279
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月21日-PDO操作数据库-***线上九期班
邯郸易住宋至刚
Original
561 people have browsed it

一、PDO操作数据库之增删改查CURD

创建连接文件:connect.php

  1. <?php
  2. $db = [
  3. 'type' => 'mysql',
  4. 'host' => '127.0.0.1',
  5. 'dbname' => 'mt',
  6. 'username' => 'root',
  7. 'password' => '111',
  8. ];
  9. $dsn = "{$db['type']}:host = {$db['host']};dbname = {$db['dbname']}";
  10. try{
  11. $pdo = new PDO($dsn,$db['username'],$db['password']);
  12. echo "Connection Success!";
  13. }catch (PDOException $e){
  14. die('Connection Failed!' . $e -> getMessage());
  15. }

1、新增记录

代码:

  1. <?php
  2. //1.连接
  3. require __DIR__.'/connect.php';
  4. //2.创建sql语句模板
  5. $sql = 'INSERT INTO `category` SET `name` = :name , `alias` = :alias';
  6. //3.创建sql语句对象
  7. $stmt = $pdo ->prepare($sql);
  8. //4.绑定参数
  9. $name = 'bjst';
  10. $alias = '巴勒斯坦';
  11. $stmt ->bindParam('name',$name,PDO::PARAM_STR);
  12. $stmt ->bindParam('alias',$alias,PDO::PARAM_STR);
  13. //5.执行查询
  14. if($stmt -> execute()){
  15. if($stmt -> rowCount() > 0){
  16. echo '新增记录'.$stmt -> rowCount().'条';
  17. }
  18. }else{
  19. die($stmt -> errorInfo());
  20. }
  21. //6.关闭连接
  22. $pdo = null;

结果:

2、更新记录

代码:

  1. <?php
  2. //1.连接
  3. require __DIR__.'/connect.php';
  4. //2.创建sql语句模板
  5. $sql = 'UPDATE `category` SET `alias` = :alias WHERE `cate_id` = 4';
  6. //3.创建sql语句对象
  7. $stmt = $pdo -> prepare($sql);
  8. //4.绑定参数
  9. $alias = '巴基斯坦';
  10. $stmt ->bindParam('alias',$alias,PDO::PARAM_STR);
  11. //5.执行查询
  12. if($stmt -> execute()){
  13. if($stmt -> rowCount() > 0){
  14. echo '更新记录'.$stmt -> rowCount().'条';
  15. }
  16. }else{
  17. die($stmt -> errorInfo());
  18. }
  19. //6.关闭连接
  20. $pdo = null;

结果:

3、查询记录

代码:

  1. <?php
  2. //1.连接
  3. require __DIR__ . '/connect.php';
  4. //2.创建sql语句模板
  5. $sql = 'SELECT * FROM `category` WHERE `cate_id` > :cate_id';
  6. //3.创建sql语句对象
  7. $stmt = $pdo -> prepare($sql);
  8. //4.绑定参数
  9. $cate_id = 2;
  10. $stmt -> bindParam('cate_id',$cate_id,PDO::PARAM_INT);
  11. //5.执行查询
  12. if ($stmt -> execute()){
  13. if ($stmt -> rowCount() > 0){
  14. $cates = $stmt -> fetchAll(PDO::FETCH_BOTH);
  15. foreach ($cates as $cate){
  16. echo '<pre>';
  17. print_r($cate);
  18. }
  19. }
  20. }else{
  21. die($stmt -> errorInfo());
  22. }
  23. //6.关闭连接
  24. $pdo = null;

结果:

4、删除记录

代码:

  1. <?php
  2. //1.连接
  3. require __DIR__ . '/connect.php';
  4. //2.创建sql语句模板
  5. $sql = 'DELETE FROM `category` WHERE `cate_id` = :cate_id';
  6. //3.创建sql语句对象
  7. $stmt = $pdo->prepare($sql);
  8. //4.绑定参数
  9. $cate_id = 4;
  10. $stmt->bindParam('cate_id', $cate_id, PDO::PARAM_INT);
  11. //5.执行查询
  12. if ($stmt->execute()) {
  13. if ($stmt->rowCount() > 0) {
  14. echo '删除记录' . $stmt->rowCount() . '条';
  15. }
  16. } else {
  17. die('$stmt -> errorInfo()');
  18. }
  19. //6.关闭连接
  20. $pdo = null;

结果:

二、手抄代码:

总结

绑定参数也可以这样:
$stmt -> execute([‘name’ => $name,’alias’ => $alias]);

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