使用 Codeigniter 插入忽略 [英] INSERT IGNORE using Codeigniter

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

问题描述

我正在尝试使用 Codeigniter 和 Active Records 在 MySQL 表中插入几行.

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

PHP 代码

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

然而,这可能会导致重复的行被插入到表中.为了处理重复数据的插入,我计划使用 INSERT IGNORE 命令在行是重复的情况下不插入该行.

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.

问题:我在 Active Records 中找不到等效的 INSERT IGNORE 并且不想编辑 Active Record 类.还有其他选择吗?

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);

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

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