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