更新查询增量字段加 1 个 codeigniter [英] Update query increment field plus 1 codeigniter

查看:26
本文介绍了更新查询增量字段加 1 个 codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的函数看起来像这样(在我的模型中)

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');
}

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'

我使用的是最新版本的 MAMP

I am using the latest version of MAMP

推荐答案

-- 需要小修正

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

谢谢

这篇关于更新查询增量字段加 1 个 codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆