Blogger Information
Blog 19
fans 0
comment 0
visits 11477
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
把 php公用方法库中的 数据库操作函数, 重写到 Db类中 - PHP培训九期线上班
Original
793 people have browsed it
  1. <?php
  2. class Db{
  3. // 连接参数
  4. public $dsn;
  5. public $user;
  6. public $password;
  7. // 连接属性
  8. public $pdo;
  9. public function __construct($dsn,$user,$password){
  10. $this->dsn = $dsn;
  11. $this->user = $user;
  12. $this->password = $password;
  13. $this->connect();
  14. }
  15. // 连接方法
  16. public function connect(){
  17. try{
  18. $this->pdo = new PDO($this->dsn,$this->user,$this->password);
  19. }catch (PDOException $e){
  20. die('数据库连接失败,错误信息:' . $e->getMessage());
  21. }
  22. }
  23. // 查询多条记录
  24. function select($table,$fields='*',$where='',$order='',$limit=''){
  25. // 创建SQL语句
  26. $sql = 'SELECT ';
  27. if (is_array($fields)) {
  28. foreach ($fields as $field){
  29. $sql .= $field . ', ';
  30. }
  31. }else{
  32. $sql .= $fields;
  33. }
  34. $sql = rtrim(trim($sql), ',');
  35. $sql .= ' FROM ' . $table;
  36. // 查询条件
  37. if (!empty($where)){
  38. $sql .= ' WHERE ' . $where;
  39. }
  40. // 排序方式
  41. if (!empty($order)){
  42. $sql .= ' ORDER BY ' . $order;
  43. }
  44. // 分页
  45. if (!empty($limit)){
  46. $sql .= ' LIMIT ' . $limit;
  47. }
  48. $sql .= ';';
  49. // 创建PDO预处理对象
  50. $stmt = $this->pdo->prepare($sql);
  51. // 执行查询操作
  52. if($stmt->execute()){
  53. if($stmt->rowCount()>0){
  54. $stmt -> setFetchMode(PDO::FETCH_ASSOC);
  55. return $stmt->fetchAll();
  56. }
  57. }
  58. return '查询失败';
  59. }
  60. // 查询单条记录
  61. function find($table,$fields,$where=''){
  62. $sql = 'SELECT ';
  63. if(is_array($fields)){
  64. foreach ($fields as $field){
  65. $sql .= $field . ', ';
  66. }
  67. }else{
  68. $sql .= $fields;
  69. }
  70. $sql = rtrim(trim($sql),',');
  71. $sql .= ' FROM ' . $table;
  72. if(!empty($where)){
  73. $sql .= ' WHERE ' . $where;
  74. }
  75. $sql .= ' LIMIT 1;';
  76. $stmt = $this->pdo->prepare($sql);
  77. if($stmt->execute()){
  78. if ($stmt->rowCount()>0){
  79. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  80. return $stmt->fetch();
  81. }
  82. }
  83. return '查询失败';
  84. }
  85. // 新增记录
  86. function insert($table,$data=[]){
  87. $sql = 'INSERT INTO ' . $table . ' SET ';
  88. if (is_array($data)){
  89. foreach ($data as $k => $v){
  90. $sql .= "{$k}='{$v}', ";
  91. }
  92. }else{
  93. return '要以关联数组形式传入数据';
  94. }
  95. $sql = rtrim(trim($sql),',') . ';';
  96. $stmt = $this->pdo->prepare($sql);
  97. if ($stmt->execute()){
  98. if($stmt->rowCount()>0){
  99. return '添加成功了' . $stmt->rowCount() . '条数据';
  100. }
  101. }
  102. return '添加失败';
  103. }
  104. // 更新数据
  105. function update($table,$data=[],$where=''){
  106. $sql = "UPDATE {$table} SET ";
  107. if (is_array($data)){
  108. foreach ($data as $k=>$v){
  109. $sql .= "{$k}='{$v}', ";
  110. }
  111. }
  112. $sql = rtrim(trim($sql),',');
  113. if(!empty($where)){
  114. $sql .= ' WHERE ' . $where;
  115. }
  116. $stmt = $this->pdo->prepare($sql);
  117. if ($stmt->execute()){
  118. if($stmt->rowCount()>0){
  119. return '成功修改了' . $stmt->rowCount() . '条数据';
  120. }
  121. }
  122. return '修改失败';
  123. }
  124. // 删除数据
  125. function delete($table,$where=''){
  126. $sql = 'DELETE FROM ' . $table;
  127. if(!empty($where)){
  128. $sql .= ' WHERE ' . $where;
  129. }
  130. $stmt = $this->pdo->prepare($sql);
  131. if($stmt->execute()){
  132. if($stmt->rowCount()>0){
  133. return '删除成功了' . $stmt->rowCount() . '条数据';
  134. }
  135. }
  136. return '删除失败';
  137. }
  138. // 统计数量
  139. function count_num($table,$where=''){
  140. $sql = 'SELECT COUNT(*) AS count_num FROM ' . $table;
  141. if (!empty($where)){
  142. $sql .= ' WHERE ' . $where;
  143. }
  144. $stmt = $this->pdo->prepare($sql);
  145. if($stmt->execute()){
  146. if($stmt->rowCount()>0){
  147. return $stmt->fetch(PDO::FETCH_ASSOC)['count_num'];
  148. }
  149. }
  150. return '输入条件不正确';
  151. }
  152. public function __destruct(){
  153. $this->pdo = null;
  154. }
  155. }

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