MYSQL多重插入在codeigniter [英] MYSQL multiple insert in codeigniter

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

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/779986/insert-multiple-rows-via-a-php-array-into-mysql">insert通过PHP数组多行到mysql

我知道做一个多重插入在MySQL会做这样的事情的可能性:

I know about the possibility of making a multiple insert in mySQL by doing something like this:

    foreach ($array as $manuf) {    
        $sql[] = '("'.mysql_real_escape_string($manuf['name']).'", "'.$manuf['lang'].'", "'.$mId.'")';
    }

    $this->db->query('INSERT INTO manufacturers (name, lang ,mid) VALUES ' . implode(',', $sql) );

我不知道是否有更好的方法来做到这一点,也许扩展当前DB(活动记录)库来用更少code使?

I wonder if there's a better way to do this and maybe extending the current DB (active-record) library to make it with even less code?

感谢

推荐答案

您需要清楚你的理由想插入多行的单个语句。它是性能?

You need to be clear about your reason for wanting to insert multiple rows in a single statement. Is it for performance?

框架是用于编程生产率和方便,但不一定性能。我同意@Udi Mosayev给出的答案 - 使用该框架API中最简单的用法

Frameworks are for programming productivity and convenience, but not necessarily performance. I agree with the answer given by @Udi Mosayev -- use the framework API in its simplest usage.

如果您要插入少量的行,每条语句,每个语句多行插入一行之间的差异不显着。

If you are inserting a small number of rows, the difference between inserting one row per statement and multiple rows per statement is insignificant.

如果有大量的行,你真的需要它们插入具有高性能,没有什么比的 LOAD DATA INFILE 。你尝试优化的插入正在拣了芝麻丢了西瓜的使用。即使倾倒你的PHP数组到 TMPFILE ,然后加载它 LOAD DATA 比使用插入更快。

If have a large number of rows and you really need them to insert with high-performance, nothing beats LOAD DATA INFILE. Your attempts to optimize usage of INSERT are being penny-wise and pound-foolish. Even dumping your PHP array into a tmpfile and then loading it LOAD DATA is faster than using INSERT.

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

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