Blogger Information
Blog 34
fans 0
comment 0
visits 21635
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月27日_pdo方法库封装为Db类使用 - 九期线上班
只猫
Original
700 people have browsed it

把PDO方法库封装类的形式

  1. <?php
  2. //PDOdb类封装
  3. Class MyDB{
  4. // 连接参数
  5. public $dsn;
  6. public $user;
  7. public $password;
  8. // 连接属性
  9. public $pdo;
  10. // 连接方法
  11. public function connect(){
  12. // 使用PDO方式管理数据库, 连接成功则返回PDO对象,赋值给对象属性pdo
  13. $this->pdo = new PDO($this->dsn, $this->user, $this->password);
  14. }
  15. // 希望在实例化时, 自动连接数据库, 这个需求很常见
  16. public function __construct($dsn, $user, $password){
  17. // 初始化对象属性
  18. $this->dsn = $dsn;
  19. $this->user = $user;
  20. $this->password = $password;
  21. // 自动调用对象方法,连接数据库
  22. $this->connect();
  23. }
  24. /**
  25. * 查询多条记录
  26. * @param $table
  27. * @param $fields
  28. * @param string $where
  29. * @return array
  30. */
  31. public function select($table,$fields, $where='', $order='',$limit=''){
  32. //拼接SQL语句
  33. $sql = 'SELECT ';
  34. if (is_array($fields)) {
  35. foreach ($fields as $field) {
  36. $sql .= $field.', ';
  37. }
  38. } else {
  39. $sql .= $fields;
  40. }
  41. $sql = rtrim(trim($sql),',');
  42. $sql .= ' FROM '.$table;
  43. //查询条件
  44. if(!empty($where)){
  45. $sql .= ' WHERE '.$where;
  46. }
  47. //排序条件
  48. if(!empty($order)) {
  49. $sql .= ' order by '.$order;
  50. }
  51. //分页条件
  52. if(!empty($limit)) {
  53. $sql .= ' limit '.$limit;
  54. }
  55. $sql .= ';';
  56. //创建PDO预处理对象
  57. $stmt = $this->pdo->prepare($sql);
  58. //执行查询操作
  59. if($stmt->execute()){
  60. if($stmt->rowCount()>0){
  61. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  62. //返回一个二维数组
  63. return $stmt->fetchAll();
  64. }
  65. } else {
  66. return false;
  67. }
  68. }
  69. /**
  70. * 查询单条记录
  71. * @param $table 表名
  72. * @param $fields 返回值 *
  73. * @param string $where 条件
  74. * @return array
  75. */
  76. public function find($table,$fields,$where=''){
  77. $pdo = $this->pdo;
  78. //创建SQL语句
  79. $sql = 'SELECT ';
  80. if (is_array($fields)) {
  81. foreach ($fields as $field) {
  82. $sql .= $field.', ';
  83. }
  84. } else {
  85. $sql .= $fields;
  86. }
  87. $sql = rtrim(trim($sql),',');
  88. $sql .= ' FROM '.$table;
  89. //查询条件
  90. if(!empty($where)){
  91. $sql .= ' WHERE '.$where;
  92. }
  93. $sql .= ' LIMIT 1;';
  94. //创建PDO预处理对象
  95. $stmt = $pdo->prepare($sql);
  96. //执行查询操作
  97. if($stmt->execute()){
  98. if($stmt->rowCount()>0){
  99. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  100. return $stmt->fetch();
  101. }
  102. } else {
  103. return false;
  104. }
  105. }
  106. /***
  107. *添加数据
  108. *@param $table
  109. *@param $data
  110. *@return bool
  111. ***/
  112. public function add($table,$data=[]){
  113. //创建SQL语句
  114. $sql = "INSERT INTO {$table} SET ";
  115. //组装插入语句
  116. if(is_array($data)){
  117. foreach ($data as $k=>$v) {
  118. $sql .= $k.'="'.$v.'", ';
  119. }
  120. }else{
  121. return false;
  122. }
  123. //去掉尾部逗号,并添加分号结束
  124. $sql = rtrim(trim($sql),',').';';
  125. //创建PDO预处理对象
  126. $stmt = $this->pdo->prepare($sql);
  127. //执行新增操作
  128. if($stmt->execute()){
  129. if($stmt->rowCount()>0){
  130. return true;
  131. }
  132. } else {
  133. return false;
  134. }
  135. }
  136. /**
  137. * 更新数据
  138. * @param $table
  139. * @param $data
  140. * @return bool
  141. */
  142. public function update($table,$data=[], $where='') {
  143. //创建SQL语句
  144. $sql = "UPDATE {$table} SET ";
  145. //组装修改语句
  146. if(is_array($data)){
  147. foreach ($data as $k=>$v) {
  148. $sql .= $k.'="'.$v.'", ';
  149. }
  150. }
  151. //去掉尾部逗号,并添加分号结束
  152. $sql = rtrim(trim($sql),',');
  153. //查询条件
  154. if(!empty($where)){
  155. $sql .= ' WHERE '.$where;
  156. }
  157. //创建PDO预处理对象
  158. $stmt = $this->pdo->prepare($sql);
  159. //执行新增操作
  160. if($stmt->execute()){
  161. if($stmt->rowCount()>0){
  162. return true;
  163. }
  164. } else {
  165. return false;
  166. }
  167. }
  168. /**
  169. * 删除数据
  170. * @param $db
  171. * @param $table
  172. * @param string $where
  173. * @return bool
  174. */
  175. public function real_delete($table,$where=''){
  176. //创建SQL语句
  177. $sql = "DELETE FROM {$table} ";
  178. //查询条件
  179. if(!empty($where)){
  180. $sql .= ' WHERE '.$where;
  181. }
  182. //创建PDO预处理对象
  183. $stmt = $this->pdo->prepare($sql);
  184. //执行删除操作
  185. if($stmt->execute()){
  186. if($stmt->rowCount()>0){
  187. return true;
  188. }
  189. } else {
  190. return false;
  191. }
  192. }
  193. /**
  194. * 统计数量
  195. * @param $pdo
  196. * @param $table
  197. * @param string $where
  198. * @return number
  199. */
  200. public function count_num($table,$where=''){
  201. //创建SQL语句
  202. $sql = 'SELECT count(*) as count_number FROM '.$table;
  203. //查询条件
  204. if(!empty($where)){
  205. $sql .= ' WHERE '.$where;
  206. }
  207. //创建PDO预处理对象
  208. $stmt = $this->pdo->prepare($sql);
  209. //执行查询操作
  210. if($stmt->execute()){
  211. if($stmt->rowCount()>0){
  212. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  213. $rows = $row['count_number'];
  214. return $rows;
  215. }
  216. } else {
  217. return false;
  218. }
  219. }
  220. // 析构方法
  221. public function __destruct(){
  222. $this->pdo = null;
  223. }
  224. }
  225. // 实例化
  226. $mydb = new MyDB('mysql:host=localhost;dbname=php_2019', 'root', '******');
  227. if ($mydb->pdo) {
  228. echo '<h2>连接成功</h2>';
  229. }
  230. // // 读取数据库测试
  231. // $stmt = $db->pdo->prepare('select * from user');
  232. // $stmt->execute();
  233. // foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $user) {
  234. // print_r($user); echo '<br>';
  235. // }
  236. echo '<h3>查询多条:</h3>';
  237. $res = $mydb->select('user','username');
  238. echo '<pre>';
  239. print_r($res);
  240. echo '<hr>';
  241. echo '<h3>查询一条:</h3>';
  242. $res = $mydb->find('user','username','uid=1');
  243. print_r($res);
  244. echo '<hr>';
  245. echo '<h3>添加数据:</h3>';
  246. $data = [
  247. 'username' => 'xiaohong',
  248. 'password' => '123456',
  249. ];
  250. $insert = $mydb->add('user',$data);
  251. var_dump($insert);
  252. echo '<hr>';
  253. echo '<h3>更新数据:</h3>';
  254. $data = [
  255. 'password' => '111111',
  256. ]; //这里密码的值不改,还是原来123456 居然执行结果会报null
  257. $update = $mydb->update('user',$data,'uid=4');
  258. var_dump($update);
  259. echo '<hr>';
  260. echo '<h3>删除数据:</h3>';
  261. $delete = $mydb->real_delete('user','uid=4');
  262. var_dump($delete);
  263. echo '<hr>';
  264. echo '<h3>统计:</h3>';
  265. $count = $mydb->count_num('user');
  266. echo $count;
  267. echo '<pre>';

运行截图:

总结:温故而知新,只要写了就会有收获。方法库改为类这个作业确实很简单,不会写的时候就是缺少那个思想。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!