Blogger Information
Blog 22
fans 0
comment 0
visits 11441
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示CURD
没耐心的渔翁
Original
620 people have browsed it

PHP CURD

什么是CURD

  • 读操作 :select;
  • 写操作: insert(增加), update(更新).delete(删除)
  • 简称:CURD,增删改查

    INSERT 增加

    1. $sql= 'INSERT `staff` SET `name` = ? , `sex` = ? , `email` = ? ';
    2. //1.创建SQL模板
    3. $stmt = $db->prepare($sql);
    4. // var_dump($stmt);
    5. echo '<br>';
    6. //2. 执行SQL
    7. // 单条数据插入
    8. $stmt->execute(['杨过','1','yangguo@qq.com']);
    9. //多条数据插入
    10. $stmt->execute(['天宁','1','tianming@qq.com']);
    11. $stmt->execute(['老冒','1','laomo@qq.com']);
    12. $stmt->execute(['红红','1','honghong@qq.com']);
    13. $stmt->execute(['小龙女','0','xiaolongnv@qq.com']);
    14. //上线后
    15. //$stmt->rowCount(): 返回受影响的记录数量
    16. if($stmt->rowCount() > 0){
    17. //查看主键id 用
    18. echo '新增成功 ' . $db->LastInsertId();
    19. }else{
    20. echo '新增失败';
    21. print_r($stmt->errorInfo());
    22. }

    CURD UPDATE (修改)

    1.连接数据库
    require DIR.’/confint/db.php’;
    //2.CURD UPDATE (修改)

    // UPDATE 表命 SET 字段1=值1 … WHERE 更新的条件
    $sql= ‘UPDATE staff SET name = ? WHERE id = ?’;
    //1.创建SQL模板
    $stmt = $db->prepare($sql);

    $stmt->execute([‘建红’,51]);

// var_dump( $stmt->execute([‘建红’,50]));
// die;
if ($stmt->rowCount() > 0) {
echo ‘更新成功’;
} else {
echo ‘更新失败’;
print_r($stmt->errorInfo());
} ```

CURD DELETE (删除)

  1. // DELETE FROM 表命 ... WHERE 条件
  2. $sql= 'DELETE FROM `staff` WHERE `id` = ? ';
  3. // '?': 匿名占位符
  4. // '?': 匿名占位符
  5. // $sql= 'DELETE FROM `staff` WHERE `id` = :id ';
  6. $stmt = $db->prepare($sql);
  7. // '?': 匿名占位符 插值
  8. $stmt->execute([$id]);
  9. // '?': 匿名占位符 插值
  10. // $stmt->execute([':id'=>55]);
  11. if ($stmt->rowCount() > 0) {
  12. echo '更新成功';
  13. } else {
  14. echo '更新失败';
  15. print_r($stmt->errorInfo());
  16. ## .CURD SELECT (单条查询)
  17. //1.连接数据库
  18. require __DIR__.'/confint/db.php';
  19. //2.CURD SELECT (单条查询)
  20. // SELECT 字段名 FROM 表名.. WHERE 更新的条件
  21. $sql= 'SELECT `id`,`name` FROM `staff` WHERE `id` > ?';
  22. $stmt = $db->prepare($sql);
  23. $stmt->execute([50]);
  24. //单条查询fetch
  25. // $staff = $stmt->fetch();
  26. // printf('<pre>%s</pre>',print_r($staff,true));
  27. // //获取关联部分
  28. // $staff = $stmt->fetch(PDO::FETCH_ASSOC);
  29. // printf('<pre>%s</pre>',print_r($staff,true));
  30. // $staff = $stmt->fetch(PDO::FETCH_ASSOC);
  31. // printf('<pre>%s</pre>',print_r($staff,true));
  32. $staff = $stmt->fetch();
  33. printf('<pre>%s</pre>',print_r($staff,true));

描述PDO的本质与原理是什么?为什么要用预处理?

  • 为什么要用预处理
    • 1.防止SQL注入攻击
    • 2.数据延迟绑定(编程是只写SQL语句模板,执行SQL时再给占位符绑定真实数据)
    • 预处理过程:
    • 1.创建SQL语句模板:数据使用占位符表示
    • 2.执行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