Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用活动记录模式在codeigniter中运行此MySQL查询_Php_Mysql_Codeigniter_Activerecord - Fatal编程技术网

Php 如何使用活动记录模式在codeigniter中运行此MySQL查询

Php 如何使用活动记录模式在codeigniter中运行此MySQL查询,php,mysql,codeigniter,activerecord,Php,Mysql,Codeigniter,Activerecord,我正在使用ci并使用其活动记录模式访问数据库。我想使用如下语句更新表 UPDATE employee_barcode set `count` = `count` + 1 where barcode_id = 2 我试着像这样使用update语句 $data = array( 'count' => 'count' + 1, ); $this->db->where('barcode_id', 2); $this->db->up

我正在使用ci并使用其活动记录模式访问数据库。我想使用如下语句更新表

UPDATE employee_barcode set `count` = `count` + 1
where barcode_id = 2
我试着像这样使用update语句

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

$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode', $data);
但结果是错误的

我该怎么做呢?

这不起作用,因为$This->db->update无法获取count的值,因此无法向其中添加1。您应该使用$this->db->select获得计数值,然后继续更新该值

例如:

$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)));
试试这个

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