Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Codeigniter:mysql事务和受影响的_行_Mysql_Codeigniter_Transactions_Rows Affected - Fatal编程技术网

Codeigniter:mysql事务和受影响的_行

Codeigniter:mysql事务和受影响的_行,mysql,codeigniter,transactions,rows-affected,Mysql,Codeigniter,Transactions,Rows Affected,我需要进行多次插入/更新,所以我提出了一个事务,以便在出现任何错误时回滚。此外,如果记录已经存在,我的应用程序应该更新记录,如果不存在,则插入记录 因此,首先,我尝试使用唯一id更新记录,如果它返回受影响的_rows=0,我将继续使用insert 可能我遗漏了事务/受影响行中的某些内容,它总是返回受影响的行=0 代码如下: $this->db->trans_start(); $this->db->where('order_id', $order_id); $

我需要进行多次插入/更新,所以我提出了一个事务,以便在出现任何错误时回滚。此外,如果记录已经存在,我的应用程序应该更新记录,如果不存在,则插入记录

因此,首先,我尝试使用唯一id更新记录,如果它返回受影响的_rows=0,我将继续使用insert

可能我遗漏了事务/受影响行中的某些内容,它总是返回受影响的行=0

代码如下:

$this->db->trans_start();

   $this->db->where('order_id', $order_id);
   $this->db->where('order_status', 'Active');
   $this->db->update('orders', $order);

   $this->db->where('order_id', $order_id);
   $this->db->where('sku', $item_sku);
   $this->db->update('order_items', $order_items);

$this->db->trans_complete();

if ($this->db->affected_rows()==0){
   $this->db->trans_start();
       $this->db->insert('orders', $order);
       $this->db->insert('order_items', $order_items);
   $this->db->trans_complete();
}

提前谢谢

我在重复密钥更新中使用MySQL的
来处理这种情况。但是,CodeIgniter的ActiveRecord实现本机不支持这一点,并且此线程中接受的答案是:


好像死了。因此,考虑只使用原始MySQL而不是ActureCordD模式;这在CI开发人员中相当常见(我不使用他们的AR实现)。

Thanx感谢您的帮助Kyle!无法在此访问死线程:。不管怎样,直接查询就可以了,所以我将使用这个解决方案!但是,如果我想在重复键上更新只在某些行上运行,例如foo='1',该怎么办?没有关于条件语句的线索,抱歉。