博客列表 >11月27日_将PHP公用方法库中的数据库操作函数重写到Db类中

11月27日_将PHP公用方法库中的数据库操作函数重写到Db类中

fkkf467
fkkf467原创
2019年11月30日 21:39:28784浏览

一、将PHP公用方法库中的数据库操作函数重写到Db类中

1. Db类

  1. class Db{
  2. // 连接参数
  3. public $dsn;
  4. public $user;
  5. public $password;
  6. // 连接属性
  7. public $pdo;
  8. // 连接方法
  9. public function connect(){
  10. try{
  11. $this->pdo = new PDO($this->dsn,$this->user,$this->password);
  12. }catch (PDOException $e){
  13. die('数据库连接失败,错误信息:' . $e->getMessage());
  14. }
  15. }
  16. // 通过构造方法实现自动连接数据库
  17. public function __construct($dsn,$user,$password){
  18. $this->dsn = $dsn;
  19. $this->user = $user;
  20. $this->password = $password;
  21. $this->connect();
  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. }

2. 查询多条数据

  1. $db = new Db('mysql:host=localhost;dbname=test','root','root');
  2. echo '<pre>' . print_r($db->select('computer','computer_name,computer_money','','computer_money DESC'),true);


3. 查询单条数据

  1. $db = new Db('mysql:host=localhost;dbname=test','root','root');
  2. echo '<pre>' . print_r($db->find('computer','computer_name,computer_money','computer_id=3'),true);


4. 新增数据

  1. $db = new Db('mysql:host=localhost;dbname=test','root','root');
  2. echo $db->insert('computer',['computer_name'=>'戴尔','computer_money'=>8999]);


5. 更新数据

  1. $db = new Db('mysql:host=localhost;dbname=test','root','root');
  2. echo $db->update('computer',['computer_money'=>7999],'computer_id=3');


6. 删除数据

  1. $db = new Db('mysql:host=localhost;dbname=test','root','root');
  2. echo $db->delete('computer','computer_id=2');


7. 统计数量

  1. $db = new Db('mysql:host=localhost;dbname=test','root','root');
  2. echo $db->count_num('computer');





二、总结

通过将数据库操作函数重写到类中,大大的方便了代码的执行,使数据库操作更便捷。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议