Blogger Information
Blog 34
fans 0
comment 0
visits 22529
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月27日—用类重写PDO操作函数
曾龙宇
Original
564 people have browsed it
  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. class Db{
  4. public $dsn;
  5. public $user;
  6. public $pwd;
  7. public $pdo;
  8. public function __construct($dsn,$user,$pwd){
  9. $this->dsn = $dsn;
  10. $this->user = $user;
  11. $this->pwd = $pwd;
  12. $this->connect();
  13. }
  14. public function connect(){
  15. $this->pdo = new PDO($this->dsn,$this->user,$this->pwd);
  16. }
  17. public function select($sql){
  18. $stmt = $this->pdo->prepare($sql);
  19. if($stmt->execute()){
  20. $arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
  21. return $arr;
  22. }else{
  23. return false;
  24. }
  25. }
  26. public function find($sql){
  27. $stmt = $this->pdo->prepare($sql);
  28. if ($stmt->execute()){
  29. if ($stmt->rowCount()>0){
  30. $arr = $stmt->fetch(PDO::FETCH_ASSOC);
  31. return $arr;
  32. }else{
  33. return false;
  34. }
  35. }else{
  36. return false;
  37. }
  38. }
  39. public function insert($sql){
  40. $stmt = $this->pdo->prepare($sql);
  41. if ($stmt->execute()){
  42. if ($stmt->rowCount()>0){
  43. return true;
  44. }else{
  45. return false;
  46. }
  47. }else{
  48. return false;
  49. }
  50. }
  51. public function update($sql){
  52. $stmt = $this->pdo->prepare($sql);
  53. if ($stmt->execute()){
  54. if ($stmt->rowCount()>0){
  55. return true;
  56. }else{
  57. return false;
  58. }
  59. }else{
  60. return false;
  61. }
  62. }
  63. public function delete($sql){
  64. $stmt = $this->pdo->prepare($sql);
  65. if ($stmt->execute()){
  66. if ($stmt->rowCount()>0){
  67. return true;
  68. }else{
  69. return false;
  70. }
  71. }else{
  72. return false;
  73. }
  74. }
  75. public function rows_count($sql){
  76. $stmt = $this->pdo->prepare($sql);
  77. if ($stmt->execute()){
  78. if ($stmt->rowCount()>0){
  79. $arr = $stmt->fetch(PDO::PARAM_INT);
  80. $row = $arr[0];
  81. return $row;
  82. }else{
  83. return false;
  84. }
  85. }else{
  86. return false;
  87. }
  88. }
  89. }
  90. $db = new Db('mysql:host=localhost;dbname=demo','root','root');
  91. $selectSql = 'SELECT * FROM `student`';
  92. $findSql = 'SELECT * FROM `student` WHERE studentId=4';
  93. $insertSql = 'INSERT INTO `student` SET `name`="测试",`phone`="123456",`collegeId`=3';
  94. $updateSql = 'UPDATE `student` SET `name`="测试",`phone`="123456",`collegeId`=3 WHERE studentId=7';
  95. $deleteSql = 'DELETE FROM `student` WHERE studentId=8';
  96. $rowSql = 'SELECT count(*) FROM `student`';
  97. //查询所有数据
  98. $selectArr = $db->select($selectSql);
  99. if ($selectArr===false){
  100. print_r($selectArr);
  101. }else{
  102. echo '查找全部数据失败';
  103. }
  104. //查询单条数据
  105. $findArr = $db->find($findSql);
  106. if ($findArr===false){
  107. print_r($findArr);
  108. }else{
  109. echo '查找数据失败';
  110. }
  111. //新增数据
  112. $insert = $db->insert($insertSql);
  113. if ($insert){
  114. echo '新增成功';
  115. }else{
  116. echo '新增失败';
  117. }
  118. //修改数据
  119. $update = $db->update($updateSql);
  120. if ($update){
  121. echo '编辑成功';
  122. }else{
  123. echo '编辑失败';
  124. }
  125. //删除数据
  126. $delete = $db->delete($deleteSql);
  127. if ($delete){
  128. echo '删除成功';
  129. }else{
  130. echo '删除失败';
  131. }
  132. //查询总记录数
  133. $row = $db->rows_count($rowSql);
  134. echo '共有:'.$row.'条数据';

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