MYSQL multiple insert in codeigniter [duplicate]
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
.
Of course. Just use $this->db->insert('dbTableName', $arrayOfData)
. The array of data is field->value, and field is the column name in you table inside the DB.
you can read more about it here
If you are going to insert really big number of rows you should do it in single query. I don't think that CI is doing it right. PS:Don't forget about mysql maximum query size.
精彩评论