INSERT忽略使用codeigniter [英] INSERT IGNORE using Codeigniter

查看:203
本文介绍了INSERT忽略使用codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用codeigniter和活动记录插入几行到MySQL表。

PHP code

  $数据=阵列('......'); //一些数据行插入
$这个 - > DB-> insert_batch(my_table的',$数据);
 

不过,这可能会导致重复的行被插入到表中。处理重复数据的插入,我打算使用插入忽略命令不会插入该行,如果该行是重复的。

问题:我无法找到插入忽略等值的活动记录,不想要编辑的活动记录类。还有没有其他办法?

下面看起来很有趣,但如果我这样做时,惯于查询运行两次?

  $ insert_query = $这个 - > DB-> insert_batch(my_table的',$数据); //查询运行ONCE
$ insert_query = str_replace函数(INSERT INTO,INSERT INTO IGNORE,$ insert_query);
$这个 - > DB->查询($ insert_query); //查询运行第二次
 

解决方案

使用你的第二个想法的技术,你可以通过遍历数组,并使用生成的查询:

  $这个 - > DB->查询($ QUERY_STRING);
 

I am trying to insert a few rows into the MySQL table using Codeigniter and Active Records.

PHP Code

$data = array('......');  // some rows of data to insert
$this->db->insert_batch('my_table', $data);

However this may cause duplicate rows to be inserted into the table. To handle the insertion of duplicate data, I plan to use the INSERT IGNORE command to not insert the row if the row is a duplicate.

Problem: I cannot find the INSERT IGNORE equivalent in Active Records and do not want to edit the Active Record class. Are there any other alternatives?

The following looks interesting, but if I do the following, wont the query be run twice?

$insert_query = $this->db->insert_batch('my_table', $data);  // QUERY RUNS ONCE
$insert_query = str_replace('INSERT INTO','INSERT IGNORE INTO',$insert_query);
$this->db->query($insert_query); // QUERY RUNS A SECOND TIME

解决方案

Using the technique of your second idea, you could generate a query by looping over the array and using:

$this->db->query($query_string);

这篇关于INSERT忽略使用codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆