Codeigniter使用增量更新mysql数据


Codeigniter update mysql data using increment

我想在名为SIZE(整数)的列中添加1,这是我的查询:

$this->dbsections->update('sections', "SIZE = SIZE + 1");

但在错误消息中,它显示为:

UPDATE `sections` SET `SIZE = SIZE +` 1 = '' WHERE `NAME` = 'ABC'

您可以重写更新查询

$this->dbsections->set('SIZE', 'SIZE+1', FALSE);// third parameter FALSE
$this->dbsections->where('NAME', "ABC");
$this->dbsections->update('sections');

set()还将接受可选的第三个参数($escape),即如果设置为FALSE ,将防止数据被转义

读取https://www.codeigniter.com/userguide3/database/query_builder.html#updating-数据

你可以试试这个:

$this->dbsections->query("UPDATE sections SET SIZE = SIZE + 1 WHERE NAME = 'ABC'");