Blogger Information
Blog 29
fans 0
comment 0
visits 19517
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示数据库的CURD操作及PDO的本质与原理
千里马遇伯乐
Original
750 people have browsed it

1. 实例演示数据库的CURD操作

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. // 1.连接数据库
  5. //DNS:数据库配置参数,类型、主机名、数据库名、端口号、默认字符集
  6. $username = 'root';
  7. $password = 'root';
  8. $dsn = 'mysql:host=127.0.0.1;dbname=statt;port=3306;charset=utf8';
  9. $db = new PDO($dsn,$username,$password);
  10. //设置查询数据只获取关联数组
  11. $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
  12. // var_dump($db);
  13. // 2.增删改查:CURD
  14. /**
  15. * PDO预处理:1.防止sql注入。2.数据延迟绑定
  16. * (编程时只写sql模板,执行sql语句在给占位符绑定真实数据)
  17. * 预处理过程:
  18. * 1.创建语句模板对象:数据用占位符表示
  19. * 2.执行sql语句,根据操作类型(写/读),读返回结果集/数组,写返回受影响的记录数量
  20. * '?'匿名占位符; 'string'命名占位符
  21. */
  22. //INSERT插入
  23. //INSERT 表名 set 字段=值
  24. // 1.关键字全大写
  25. // 2.表名字段名使用反引号作为定界符
  26. $sql = 'INSERT `user` SET `name` = ?, `sex` = ?, `email` = ?';
  27. // print($sql);
  28. //1.创建sql语句模板对象
  29. $statt = $db->prepare($sql);
  30. //2.执行sql语句
  31. //成功
  32. $statt->execute(['洪七公',0,'gongqigong@php.cn']);
  33. $statt->execute(['小龙女',1,'xiaolongnv@php.cn']);
  34. $statt->execute(['李莫愁',1,'limochou@php.cn']);
  35. if($statt->rowCount()>0){
  36. echo '新增成功,ID=' . $db->lastInsertId();
  37. }else{
  38. echo '新增失败';
  39. print_r($statt->errorInfo());
  40. }
  41. if($statt->execute(['杨过',0,'yangguo@php.cn'])){
  42. }else{
  43. //失败
  44. echo '执行失败';
  45. print_r($statt->errorInfo());
  46. }
  47. //UPDATE更新操作
  48. $sql = 'UPDATE `user` SET `name` = ? WHERE `id` = ?';
  49. $statt = $db->prepare($sql);
  50. $statt->execute(['小龙女22',3]);
  51. if($statt->rowCount()>0){
  52. echo '更新成功';
  53. }else{
  54. echo '更新失败';
  55. print_r($statt->errorInfo());
  56. }
  57. //DELETE删除操作
  58. $sql = 'DELETE FROM `user` WHERE `id` = ?';
  59. $statt = $db->prepare($sql);
  60. $statt->execute([3]);
  61. if($statt->rowCount()>0){
  62. echo '删除成功';
  63. }else{
  64. echo '删除失败';
  65. print_r($statt->errorInfo());
  66. }
  67. //SELECT 查询操作
  68. $sql = 'SELECT * FROM `user`';
  69. $statt = $db->prepare($sql);
  70. $statt->execute();
  71. //单条查询fetch
  72. // $staff = $statt->fetch();
  73. // printf('<pre>%s</pre>',print_r($staff,true));
  74. //多条查询fetchAll
  75. $staff = $statt->fetchAll();
  76. printf('<pre>%s</pre>',print_r($staff,true));

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

  1. PDO预处理:1.防止sql注入。2.数据延迟绑定
  2. (编程时只写sql模板,执行sql语句在给占位符绑定真实数据)
  3. 预处理过程:
  4. 1.创建语句模板对象:数据用占位符表示
  5. 2.执行sql语句,根据操作类型(写/读),读返回结果集/数组,写返回受影响的记录数量
  6. '?'匿名占位符; 'string'命名占位符
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