简体   繁体   中英

How to update the column with increment value

I have used this query to increment my column with the selected number like

$this->db->where('my Condition');
$this->db->update('my Table',array('name'=>'gautam','count'=>'2'));

Here I want to add 2 to the actual column value of count . But I can't able to do it with update function.And I cont able to do like

$this->db->update('my Table',array('name'=>'gautam','count'=>'count+2'));

because I'm only getting count of "2" and if I add in my query it is adding ' in my query like

enter code here

Can anyone help me to find out the solution for it.

$this->db->set('name', 'gautam');
$this->db->set('count', 'count+2',FALSE);
$this->db->where('my Condition');
$this->db->update('my Table');

$this->db->set() in Codeigniter

Try this :

$this->db->where('my Condition');
$this->db->set(array('count' => 'count+2', 'name' => 'gautam'), FALSE);
$this->db->update('my Table');

You can use Codeigniter set function to set the value of count with the increment . From the doc

set() will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE. To illustrate the difference, here is set() used both with and without the escape parameter.

So you can do something like this :

$this->db->where('my Condition');
$this->db->set('count','count+2',FALSE);//SET COUNT WITH COUNT+2
$this->db->set('name','gautam');
$this->db->update('my Table');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM