MYSQL在codeigniter中多次插入

编程入门 行业动态 更新时间:2024-10-09 17:28:58
本文介绍了MYSQL在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)

我不知道有没有更好的方法来做这个,也许扩展当前的数据库

$ 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中多次插入

本文发布于:2023-10-19 17:43:48,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MYSQL   codeigniter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!