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 ofINSERTare being penny-wise and pound-foolish. Even dumping your PHP array into a tmpfile and then loading itLOAD DATAis faster than usingINSERT.