MYSQL在codeigniter中多次插入 [英] MYSQL multiple insert in codeigniter
问题描述
我知道有可能通过这样做在mySQL中进行多重插入:
foreach ($ array as $ manuf){
$ sql [] ='('.mysql_real_escape_string($ manuf ['name'])。','。$ manuf ['lang']。 '。$ mId。')';
}
$ this-> db-> query('INSERT INTO manufacturer(name,lang,mid)VALUES'。implode(',',$ sql)
我不知道有没有更好的方法来做这个,也许扩展当前的数据库
您需要清楚地知道您希望在单个语句中插入多行的原因。是为了性能吗?
框架是用于编程的生产力和方便,但不一定是性能。我同意@Udi Mosayev给出的答案 - 在最简单的用法中使用框架API。
如果你插入少量行,
如果有大量的行,并且你真的需要他们以高性能插入,没有什么节拍 载入资料INFILE
。您尝试优化 INSERT
的使用方法是一分钱一分钱和愚蠢的。甚至将您的PHP数组转储到 tmpfile ,然后加载它 LOAD DATA
比使用 INSERT
更快。
Possible Duplicate:
insert multiple rows via a php array into 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) );
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?
Thanks
You need to be clear about your reason for wanting to insert multiple rows in a single statement. Is it for performance?
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.
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屋!