实现更新,删除,插入操作的链式调用

编辑: admin 分类: 电脑知识 发布时间: 2023-06-14 来源:互联网
实现更新,删除,插入操作的链式调用data方法
  1. public function Data($Data){
  2. $str = '';
  3. if (count($Data) > 0) {
  4. foreach ($Data as $k => $v) {
  5. $str .= ' , ' . $k . '= "' . $v.'"';
  6. }
  7. $str = substr($str, 2);
  8. }
  9. $this->opts['Data'] = " $str";
  10. return $this;
  11. }
更新
  1. public function update()
  2. {
  3. $sql = 'UPDATE ' .$this->table .' SET ';
  4. $sql .= $this->opts['Data']??null;
  5. $sql .= $this->opts['where']??die('禁止无条件删除');
  6. $stmt = $this->db->prepare($sql);
  7. $res = $stmt->execute();
  8. return $stmt->rowCount();
  9. }
删除
  1. public function delete()
  2. {
  3. $sql = 'DELETE FROM '.$this->table;
  4. $sql .= $this->opts['where']??die('禁止无条件删除');
  5. $stmt = $this->db->prepare($sql);
  6. $stmt->execute();
  7. return $stmt->rowCount();
  8. }
插入
  1. public function insert()
  2. {
  3. $sql = 'INSERT '.$this->table.' SET ';
  4. $sql .= $this->opts['Data'];
  5. $stmt = $this->db->prepare($sql);
  6. $stmt->execute();
  7. return $stmt->rowCount();
  8. }
客户端调用代码
  1. echo Db::table('T_product')
  2. ->where(['id'=>1])
  3. ->Data(['proname'=>'空调','description'=>'格力空调'])
  4. ->update();
  5. echo Db::table('T_product')
  6. ->where(['id'=>10])
  7. ->delete();
  8. echo Db::table('T_product')
  9. ->Data(['proid'=>'120058','proname'=>'平板','price'=>9988,'description'=>'平板2'])
  10. ->insert();
【转自:美国cn2服务器 http://www.558idc.com/mg.html欢迎留下您的宝贵建议】