Blogger Information
Blog 53
fans 3
comment 0
visits 55250
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月27日-把PHP中公用方法库中的数据库操作函数重写到Db类中-***线上九期班
邯郸易住宋至刚
Original
589 people have browsed it

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

代码:

  1. <?php
  2. namespace _1215;
  3. use PDO;
  4. // 数据库查询类
  5. class Db
  6. {
  7. // 连接对象
  8. public $pdo = null;
  9. // 数据表名
  10. public $table = '';
  11. // 字段列表
  12. public $field = '';
  13. // 查询条件
  14. public $where = '';
  15. // 显示数量
  16. public $limit = 0;
  17. // 构造方法,初始化连接对象
  18. public function __construct($pdo)
  19. {
  20. // 连接对象是对象方法的共享属性
  21. $this->pdo = $pdo;
  22. }
  23. // 调用表名
  24. public function table($tableName)
  25. {
  26. $this->table = $tableName;
  27. // 返回当前对象,便于链式调用该对象的其它方法
  28. return $this;
  29. }
  30. // 设置查询字段
  31. public function field($fields=[])
  32. {
  33. $this->field = $fields;
  34. return $this;
  35. }
  36. // 设置查询条件
  37. public function where($where)
  38. {
  39. $this->where = $where;
  40. return $this;
  41. }
  42. // 设置显示数量
  43. public function limit($limit)
  44. {
  45. $this->limit = $limit;
  46. return $this;
  47. }
  48. // 创建SQL查询语句对象,并返回查询结果
  49. public function select()
  50. {
  51. // 查询条件分开设置, 可以确保链式方法独立
  52. $fields = empty($this->field) ? '*' : $this->field;
  53. $where = empty($this->where) ? '' : ' WHERE '.$this->where;
  54. $limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
  55. // 接装SQL语句
  56. $sql = 'SELECT '.$fields.' FROM '.$this->table. $where . $limit;
  57. // 预处理查询
  58. $stmt = $this->pdo->prepare($sql);
  59. $stmt->execute();
  60. //return $stmt->fetchAll(PDO::FETCH_ASSOC);
  61. }
  62. public function insert($data=[])
  63. {
  64. // 查询条件分开设置, 可以确保链式方法独立 INSERT INTO `category` SET `name`='rh', `alias`='欧美猛片';
  65. $fields = empty($this->field) ? '*' : $this->field;
  66. $where = empty($this->where) ? '' : ' WHERE '.$this->where;
  67. $limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
  68. // 接装SQL语句
  69. //$sql = 'INSERT INTO '.$this->table.' SET '.'('.'`'.$fields[0].'`'.','.'`'.$fields[1].'`'.') VALUES '.'('.'`'.$data[0].'`'.','.'`'.$data[1].'`'.');
  70. $sql = 'INSERT INTO '.$this->table.' SET '.'`'.$fields[0].'`'.' = '.'\''.$data[0].'\''.','.'`'.$fields[1].'`'.' = '.'\''.$data[1].'\'';
  71. var_dump($sql);
  72. // 预处理查询
  73. $stmt = $this->pdo->prepare($sql);
  74. $stmt->execute();
  75. if ($stmt->rowCount()>0){
  76. echo '插入数据 '.$stmt->rowCount().'条';
  77. }else{
  78. echo '插入数据失败';
  79. }
  80. }
  81. public function update($data)
  82. {
  83. // 查询条件分开设置, 可以确保链式方法独立 UPDATE category SET `alias`='日韩新片' WHERE cate_id = 3
  84. $fields = empty($this->field) ? '*' : $this->field;
  85. $where = empty($this->where) ? '' : ' WHERE '.$this->where;
  86. $limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
  87. // 接装SQL语句
  88. $sql = 'UPDATE '.$this->table. ' SET '.'`'.$fields .'`'.' = '.'\''.$data.'\''.$where;
  89. var_dump($sql);
  90. // 预处理查询
  91. $stmt = $this->pdo->prepare($sql);
  92. $stmt->execute();
  93. //return $stmt->fetchAll(PDO::FETCH_ASSOC);
  94. if ($stmt->rowCount()>0){
  95. echo '更新数据成功 '.$stmt->rowCount().'条';
  96. }else{
  97. echo '更新数据失败';
  98. }
  99. }
  100. public function delete()
  101. {
  102. // 查询条件分开设置, 可以确保链式方法独立 DELETE FROM movies WHERE mov_id = 9
  103. $fields = empty($this->field) ? '*' : $this->field;
  104. $where = empty($this->where) ? '' : ' WHERE '.$this->where;
  105. $limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
  106. // 接装SQL语句
  107. $sql = 'DELETE FROM '.$this->table.$where;
  108. var_dump($sql);
  109. // 预处理查询
  110. $stmt = $this->pdo->prepare($sql);
  111. $stmt->execute();
  112. //return $stmt->fetchAll(PDO::FETCH_ASSOC);
  113. if ($stmt->rowCount()>0){
  114. echo '删除数据成功 '.$stmt->rowCount().'条';
  115. }else{
  116. echo '删除数据失败';
  117. }
  118. }
  119. }
  120. $db = new Db(new PDO('mysql:host=127.0.0.1;dbname=mt','root','111'));

CURD操作

1、新增记录

  1. $result = $db->table('user')
  2. ->field([name,age])
  3. ->insert([hhappysong,50]);
  4. var_dump($result)

结果:


2、更新记录

  1. $result = $db->table('user')
  2. ->field('name')
  3. ->where('uid = 12')
  4. ->update('任双海');
  5. var_dump($result);

结果:


3、查询记录

  1. $res = $db->table('user')
  2. ->where('uid>2')
  3. ->delete();
  4. var_dump($res);

结果:


4、删除记录

  1. $res = $db->table('user')
  2. ->where('uid=9')
  3. ->delete();
  4. var_dump($res)

结果:


总结

SQL语句拼接中的问题

在SQL语句拼接中,新增记录语句拼接很困难,错误百出,勉强能用但是还是报错

解决思路:

新增记录SQL语句中,需要用到键值对形式,因此设想把字段名$fields和需要新增数据$data设置成数组$fields=[],$data=[],然后按照两个数组一一对应,这时能够生成正确的SQL语句,也能正常插入新数据,但是报错;

代码如下:

  1. public function insert($data=[])
  2. {
  3. // 查询条件分开设置, 可以确保链式方法独立 INSERT INTO `category` SET `name`='rh', `alias`='欧美猛片';
  4. $fields = empty($this->field) ? '*' : $this->field;
  5. $where = empty($this->where) ? '' : ' WHERE '.$this->where;
  6. $limit = empty($this->limit) ? '' : ' LIMIT '.$this->limit;
  7. // 接装SQL语句
  8. //$sql = 'INSERT INTO '.$this->table.' SET '.'('.'`'.$fields[0].'`'.','.'`'.$fields[1].'`'.') VALUES '.'('.'`'.$data[0].'`'.','.'`'.$data[1].'`'.');
  9. $sql = 'INSERT INTO '.$this->table.' SET '.'`'.$fields[0].'`'.' = '.'\''.$data[0].'\''.','.'`'.$fields[1].'`'.' = '.'\''.$data[1].'\'';
  10. var_dump($sql);
  11. // 预处理查询
  12. $stmt = $this->pdo->prepare($sql);
  13. $stmt->execute();
  14. if ($stmt->rowCount()>0){
  15. echo '插入数据 '.$stmt->rowCount().'条';
  16. }else{
  17. echo '插入数据失败';
  18. }
  19. }

提示错误如下:

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