可能重复:
我知道有可能通过这样做在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)我不知道有没有更好的方法来做这个,也许扩展当前的数据库
$ p 解决方案您需要清楚地知道您希望在单个语句中插入多行的原因。是为了性能吗?
框架是用于编程的生产力和方便,但不一定是性能。我同意@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中多次插入
发布评论