Blogger Information
Blog 46
fans 0
comment 0
visits 34320
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP中实例演示CURD操作
上草一方
Original
666 people have browsed it

代码如下:

  1. <?php
  2. namespace php_edu;
  3. use PDO;
  4. /**
  5. * 数据库常用操作
  6. * 1. 读操作: select
  7. * 2. 写操作: insert,update,delete
  8. * 简称: CURD, 增删改查
  9. */
  10. // 1. 连接数据库
  11. require __DIR__ . '/config/connect.php';
  12. // 2. CURD: INSERT
  13. /**
  14. * PDO预处理
  15. * 为什么要用预处理?
  16. * 1. 防止SQL注入攻击, 2. 数据延迟绑定
  17. * (编程时只写SQL语句模板,执行SQL时再给占位符绑定真实数据)
  18. * 预处理过程:
  19. * 1. 创建SQL语句模板对象: 数据使用占位符表示
  20. * 2. 执行SQL语句,根据操作类型(写/读),读返回结果集/数组, 写返回受影响的记录数量
  21. */
  22. // INSERT 插入
  23. // INSERT 表名 SET 字段1=值1, 字段2=值2, ....
  24. // SQL语句的推荐规范:
  25. // 1. 关键字全大写
  26. // 2. 表名,字段名使用反引号做为定界符
  27. $sql = 'INSERT `myhome` SET `name` = ?, `sex` = ?, `email` = ?';
  28. // 1. 创建SQL语句模板对象
  29. $stmt = $db->prepare($sql);
  30. // var_dump($stmt);
  31. // 2. 执行SQL语句
  32. $stmt->execute(['爸爸', 0, 'father@qq.com']);
  33. $stmt->execute(['妈妈', 1, 'mother@qq.com']);
  34. $stmt->execute(['爷爷', 0, 'grandpa@qq.com']);
  35. $stmt->execute(['奶奶', 1, 'grandma@qq.com']);
  36. $stmt->execute(['哥哥', 0, 'brother@qq.com']);
  37. $stmt->execute(['姐姐', 1, 'sister@qq.com']);
  38. $stmt->execute(['我自己', 0, 'myself@qq.com']);
  39. // 成功
  40. // $stmt->rowCount(): 返回受影响的记录数量
  41. if ($stmt->rowCount() > 0) {
  42. echo '新增成功, 新增记录的主键ID = ' . $db->lastInsertId();
  43. } else {
  44. echo '新增失败';
  45. print_r($stmt->errorInfo());
  46. }
  47. // 2. CURD: UPDATE 更新
  48. $sql = 'UPDATE `myhome` SET `name` = ? WHERE `id` = ?';
  49. $stmt = $db->prepare($sql);
  50. $stmt->execute(['夏天', 1]);
  51. if ($stmt->rowCount() > 0) {
  52. echo '更新成功';
  53. } else {
  54. echo '更新失败';
  55. print_r($stmt->errorInfo());
  56. }
  57. echo '<hr>';
  58. // 3. CURD: DELETE 删除
  59. $sql = 'DELETE FROM `myhome` WHERE `id` = ?';
  60. $stmt = $db->prepare($sql);
  61. $stmt->execute([2]);
  62. if ($stmt->rowCount() > 0) {
  63. echo '删除成功';
  64. } else {
  65. echo '删除失败';
  66. print_r($stmt->errorInfo());
  67. }
  68. echo '<hr>';
  69. // 4. CURD: SELECT 单条查询
  70. // SELECT 字段列表 FROM 表名 WHERE 查询条件
  71. $sql = 'SELECT `id` ,`name` FROM `myhome` WHERE `id` > :id';
  72. $stmt = $db->prepare($sql);
  73. $stmt->execute(['id'=>6]);
  74. // 单条查询
  75. $myhome = $stmt->fetch(PDO::FETCH_ASSOC);
  76. printf('<pre>%s</pre>', print_r($myhome, true));
  77. echo '<hr>';
  78. // 5. CURD: SELECT 多条查询
  79. // SELECT 字段列表 FROM 表名 WHERE 查询条件
  80. $sql = 'SELECT `id`,`name` FROM `myhome` WHERE `id` > :id';
  81. $stmt = $db->prepare($sql);
  82. $stmt->execute(['id' => 7]);
  83. // fetchAll: 返回全部满足条件的记录集合,二维数组
  84. $myhomes = $stmt->fetchAll();
  85. // print_r($staffs);
  86. foreach ($myhomes as $myhome) {
  87. printf('<pre>%s</pre>', print_r($myhome, true));
  88. }

数据库更新如下:

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