PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

How to do Multiple SQL Inserts

Posted by teejay on November 2, 2009

Using multiple insert queries almost always slip my mind when developing applications. If you are like me, we all need some reminder in some way or another.

Inserting multiple rows using multiple inserts:

$this->db->insert('students', array('first_name' => 'teejay', 'last_name' => 'obazee'));
$this->db->insert('students', array('first_name' => 'maev', 'last_name' => 'shadowsong'));
$this->db->insert('students', array('first_name' => 'jaina', 'last_name' => 'proudmore'));

Inserting multiple rows using a single query:

$this->db->query('INSERT INTO students (first_name, last_name) VALUES
                     ('teejay', 'obazee')'
                     ('maev', 'shadowsong')'
                     ('jaina', 'proudmore')', FALSE)
Unfortunately, CodeIgniter’s Active Record cannot do multiple inserts as of this writing. This will be the solution for now.

Categories: How To, Web Development

Tags: codeigniter, sql, php, mysql

No Comments

Comments