database 使用 codeigniter 将数据库中字段的值更新为 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3304991/
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
Update the value of a field in database by 1 using codeigniter
提问by vikmalhotra
I want to implement a SQL statement using codeigniter active record.
我想使用 codeigniter 活动记录实现一条 SQL 语句。
UPDATE tags SET usage = usage+1 WHERE tag="java";
How can I implement this using Codeigniter active records?
如何使用 Codeigniter 活动记录实现此功能?
Regards
问候
回答by Phil Sturgeon
$this->db->set('usage', 'usage+1', FALSE);
$this->db->where('tag', 'java');
$this->db->update('tags');
回答by Srihari Goud
You can also use something like this
你也可以使用这样的东西
$data = array('usage' => 'usage+1', *other columns*);
$this->db->where('tag', 'java');
$this->db->update('tags', $data);
UPDATE: $data was not being passed on to update
更新:$data 没有被传递到更新
回答by Stephen Curran
I find its sometimes simpler to just write the SQL rather than having Active Record build it for me.
我发现有时只编写 SQL 而不是让 Active Record 为我构建它更简单。
$sql = 'update tags set usage=usage+1 where tag=?';
$this->db->query($sql, array($tag));