Blogger Information
Blog 38
fans 0
comment 0
visits 22824
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
更新,删除,插入操作的链式调用
一个好人
Original
821 people have browsed it

数据库操作的链式调用

插入:

  1. //insert方法
  2. public function insert()
  3. {
  4. $sql = 'INSERT ' . $this->table. ' SET ';
  5. $sql .= $this->opts['value'] ?? null;
  6. echo $sql;
  7. $stmt = $this->db->prepare($sql);
  8. $stmt->execute();
  9. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  10. }
  11. //value方法
  12. public function value(array $value=[]){
  13. $str = '';
  14. if (count($value) > 0) {
  15. foreach ($value as $k => $v) {
  16. $str .= $k . "='" . $v . "',";
  17. }
  18. //去掉最后一个","
  19. $str = substr($str, 0, strlen($str) - 1);
  20. $this->opts['value'] = " $str";
  21. return $this;
  22. }

更新:

  1. //update方法
  2. public function update()
  3. {
  4. $sql = 'UPDATE ' . $this->table. ' SET ';
  5. $sql .= $this->opts['value'] ?? null;
  6. $sql .= $this->opts['where'] ?? null;
  7. echo $sql;
  8. $stmt = $this->db->prepare($sql);
  9. $stmt->execute();
  10. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  11. }

删除:

  1. //delete方法
  2. public function delete()
  3. {
  4. $sql = 'DELETE FROM ' . $this->table;
  5. $sql .= $this->opts['where'] ?? null;
  6. echo $sql;
  7. $stmt = $this->db->prepare($sql);
  8. $stmt->execute();
  9. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  10. }

总结:

这是学习框架底层逻辑最重要的内容之一,也是比较难理解的,通过简单的sql语句拼接实现更新、删除、插入操作,还没有使用sql预处理防注入,有时间再出来吧!

Correcting teacher:PHPzPHPz

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