简体   繁体   中英

How to run this MySQL query in codeigniter using active record pattern

I am using ci and using its active record pattern to access database. I want to update table using statement like

UPDATE employee_barcode set `count` = `count` + 1
where barcode_id = 2

I tried using update statement like this

$data = array(
            'count' => 'count' + 1,
        );

$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode', $data);

But the result was wrong.

How can i do so?

This doesn't work because $this->db->update can't get the value of count , and therefore can't add 1 to it. You should get the count value using $this->db->select , then proceed to update the value.

For example:

$this->db->where('barcode_id', 2);
$this->db->select('count');
$query = $this->db->get('employee_barcode');
$row = $query->row();
$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode', array('count' => ($row->count + 1)));

Try this..

$this->db->set('count', '`count+1`', FALSE)
$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode');

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