php 更新查询增量字段加 1 个 codeigniter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12904308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:27:01  来源:igfitidea点击:

Update query increment field plus 1 codeigniter

phpcodeignitercodeigniter-2

提问by user1724347

I got a problem when trying to increment by 1 on given field in my db. I tried with and without active records.

尝试在我的数据库中的给定字段上增加 1 时遇到问题。我尝试使用和不使用活动记录。

My functions look like this (in my model)

我的功能看起来像这样(在我的模型中)

function _set_reads($id){
$this->db->set('reads', 'reads+1', FALSE)
$this->db->where('id', $id);
$this->db->update('article');
}

and

function _set_reads($id){
$sql = 'update article set reads=reads+1 where id=?';
$this->db->query($sql, array($id));
}

I get the same error in both cases and it's the following error message:

我在这两种情况下都收到相同的错误,它是以下错误消息:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'reads+1 WHERE `id` = '15'' at line 1

UPDATE `article` SET `reads` = reads+1 WHERE `id` = '15'

I am using the latest version of MAMP

我正在使用最新版本的 MAMP

回答by syyu

-- Need small correction

-- 需要小修正

$this->db->where('id', $id);
$this->db->set('set_row', '`set_row`+ 1', FALSE);

Thank You

谢谢你

回答by user1724347

Solved it: I had to change

解决了:我必须改变

$this->db->set('reads', 'reads+1', FALSE)

to

$this->db->set('reads', '`reads+1`', FALSE)

Sorry for the post...

对不起,帖子...

回答by Wahyu Kristianto

set()will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE.

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