views:

606

answers:

3

Hi everyone,

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

+1  A: 

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

Udi Mosayev
+1  A: 

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.

Kirzilla
Hi Kirzilla, Isn't that what I am doing in my example, or have I gotten it wrong? Thanks!
Industrial
Yes, you're doing it right. But if you'll use CI insert('table', $array) - I'm not sure that CI will it do as you wish. CI could execute multiple insert queries. So, be careful using CI's insert. Good luck!
Kirzilla
+2  A: 

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.

Bill Karwin
Hi Bill. Strictly for performance it is. I am aware about what Udi Mosajev linked. I have not been able to get it running with multiple rows though.To do a load data infile seems interesting though and definitely a thing to consider. I will keep looking for a way to do multiple inserts or updates within the frames of the codeigniter active record style though...
Industrial
The value of an ORM framework is for developer productivity, not runtime performance.
Bill Karwin
Hi Bill, I am aware of that, so therefore I would love to extend the database library of Codeigniter to do this. Want to thank you for your "SQL antipattern strike back" on Slideshare as well, probably one of the best things I've ever read!
Industrial
@Bill Karwin, thank you for "LOAD DATA INFILE" I didn't know about it.
Kirzilla
@Industrial: Thanks! I'm glad it was helpful. Be sure to check out my *SQL Antipatterns* book coming out very soon, with even more tips: http://www.pragprog.com/titles/bksqla/sql-antipatterns
Bill Karwin
Hi Bill, thats amazing. That has a sure place on my bookshelf now! I wonder if you would mind helping me out with: http://stackoverflow.com/questions/2794638/turn-database-result-into-arrayThanks!
Industrial