博客列表 >2019年11月27日—用类重写PDO操作函数

2019年11月27日—用类重写PDO操作函数

Rambo-Yang
Rambo-Yang原创
2019年11月28日 21:15:36526浏览
  1. <?php
  2. // 把pdo写到类中
  3. class Db
  4. {
  5. public $dsn;
  6. public $username;
  7. public $password;
  8. public $pdo;
  9. public function __construct($dsn, $username, $password)
  10. {
  11. $this->dsn = $dsn;
  12. $this->username = $username;
  13. $this->password = $password;
  14. // return new PDO($this->dsn,$this->username,$this->password);
  15. $this->connect();
  16. }
  17. public function connect()
  18. {
  19. try {
  20. $this->pdo = new PDO($this->dsn, $this->username, $this->password);
  21. } catch (PDOException $e) {
  22. die(print_r($e->getMessage(), true));
  23. }
  24. return $this->pdo;
  25. }
  26. //新增操作
  27. public function insert($table, $data = [])
  28. {
  29. //连接sql数据库
  30. //组装sql语句
  31. $sql = "INSERT INTO {$table} SET ";
  32. // print_r($data);
  33. if (is_array($data) && !empty($data)) {
  34. foreach ($data as $k => $v) {
  35. $sql .= '`' . $k . '`="' . $v . '",';
  36. }
  37. } else {
  38. return false;
  39. }
  40. $sql = rtrim(trim($sql), ',') . ';';
  41. // print_r($sql);exit();
  42. //创建预处理对象
  43. $stmt = $this->pdo->prepare($sql);
  44. //执行sql操作
  45. $res = $stmt->execute();
  46. //返回操作结果
  47. if ($res) {
  48. if ($stmt->rowCount() > 0) {
  49. return '成功添加了' . $stmt->rowCount() . '条数据';
  50. } else {
  51. return '没有新增数据';
  52. }
  53. } else {
  54. return false;
  55. }
  56. //关闭sql链接
  57. }
  58. //修改操作
  59. public function update($table, $data = [], $where = '')
  60. {
  61. $sql = "UPDATE {$table} SET ";
  62. if (is_array($data) && !empty($data)) {
  63. foreach ($data as $k => $v) {
  64. $sql .= '`' . $k . '`="' . $v . '",';
  65. }
  66. } else {
  67. return false;
  68. }
  69. $sql = rtrim(trim($sql), ',') . ' ';
  70. if (!empty($where)) {
  71. $sql .= 'WHERE ' . $where . ';';
  72. } else {
  73. return false;
  74. }
  75. // print_r($sql);exit();
  76. //创建预处理对象
  77. $stmt = $this->pdo->prepare($sql);
  78. //执行SQL操作
  79. if ($stmt->execute()) {
  80. if ($stmt->rowCount() > 0) {
  81. return '成功更新了' . $stmt->rowCount() . '条数据';
  82. } else {
  83. return '没有数据被更新';
  84. }
  85. } else {
  86. return false;
  87. }
  88. }
  89. //删除操作
  90. public function delete($table, $where = '')
  91. {
  92. $sql = "DELETE FROM {$table} ";
  93. if (!empty($where)) {
  94. $sql .= 'WHERE ' . $where . ';';
  95. } else {
  96. die('删除必须加条件');
  97. }
  98. // print_r($sql); exit();
  99. $stmt = $this->pdo->prepare($sql);
  100. if ($stmt->execute()) {
  101. if ($stmt->rowCount() > 0) {
  102. return '成功删除了' . $stmt->rowCount() . '条数据';
  103. } else {
  104. return '找不到要删除的数据';
  105. }
  106. } else {
  107. return false;
  108. }
  109. }
  110. //查询操作
  111. //查询单条数据
  112. public function find($table, $fields, $where = '')
  113. {
  114. $sql = "SELECT ";
  115. if (is_array($fields)) {
  116. foreach ($fields as $v) {
  117. $sql .= $v . ',';
  118. }
  119. } else {
  120. $sql .= $fields;
  121. }
  122. $sql = rtrim(trim($sql), ',');
  123. $sql .= ' FROM ' . $table;
  124. if (!empty($where)) {
  125. $sql .= ' WHERE ' . $where;
  126. }
  127. $sql .= ' LIMIT 1;';
  128. $stmt = $this->pdo->prepare($sql);
  129. $find = $stmt->execute();
  130. if ($find) {
  131. return $stmt->fetch(PDO::FETCH_ASSOC);
  132. } else {
  133. return false;
  134. }
  135. }
  136. //查询多条数据
  137. public function select($table, $fields, $where = '', $order = '', $limit = '')
  138. {
  139. $sql = "SELECT ";
  140. if (is_array($fields)) {
  141. foreach ($fields as $field) {
  142. $sql .= $field . ',';
  143. }
  144. } else {
  145. $sql .= $fields;
  146. }
  147. $sql = rtrim(trim($sql), ',');
  148. $sql .= ' FROM ' . $table;
  149. if (!empty($where)){
  150. $sql .= ' WHERE '. $where;
  151. }
  152. if (!empty($order)){
  153. $sql .= ' ORDER BY '.$order;
  154. }
  155. if (!empty($limit)){
  156. $sql .= ' LIMIT '.$limit . ';';
  157. }
  158. // print_r($sql);
  159. $stmt = $this->pdo -> prepare($sql);
  160. if($stmt->execute()){
  161. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  162. }
  163. }
  164. //析构函数
  165. public function __destruct()
  166. {
  167. $this->pdo = null;
  168. }
  169. }
  170. $db = new Db("mysql:host=localhost;dbname=movies", 'root', 'root');
  171. //print_r($db->pdo);
  172. //print_r($db->pdo);
  173. //if($db->pdo){
  174. // echo '连接成功';
  175. //}
  176. //插入操作
  177. //$insert = $db->insert('category',['name'=>'ds','alias'=>'都市生活剧']);
  178. //echo $insert;
  179. //更新操作
  180. //$update = $db->update('category', ['name' => 'xj', 'alias' => '喜剧片'], '`cate_id`=23');
  181. //echo $update;
  182. //删除操作
  183. //$del = $db->delete('category', '`cate_id` = 24');
  184. //echo $del;
  185. //查找单条记录
  186. //$res = $db->find('category','*','cate_id = 25');
  187. //print_r($res);
  188. //查找多条记录
  189. $sel = $db->select('category', '*','name="gz"','cate_id DESC','0,10');
  190. print_r($sel);
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议