3

Possible Duplicate:
MYSQL multiple insert in codeigniter

I would like to execute multiple insert queries simultaneously using codeignitor framework in PHP.There is any simple ways to do this without writing multiple insert queries.Already tried something like $this->db->query('INSERT INTO students (first_name, last_name) VALUES ('teejay', 'obazee')' ('maev', 'shadowsong')' ('jaina', 'proudmore')', FALSE) .There is any ways like this.Which is not working.If anybody knows;please help me.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aadi
  • 6,959
  • 28
  • 100
  • 145

3 Answers3

9

You can use codeignitor native active record

$data = array(
   array(
      'first_name' => 'teejay', 'last_name' => 'obazee'
   ),
   array(
      'first_name' => 'maev', 'last_name' => 'shadowsong'
   ),

   array(
      'first_name' => 'jaina', 'last_name' => 'proudmore'
   )
);

$this->db->insert_batch('students', $data);

It will produce query

INSERT INTO students (first_name,last_name) VALUES  ('teejay', 'obazee'),('maev', 'shadowsong'),('jaina', 'proudmore');
Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
3

I also had problem with sending multi queries in CodeIgniter.

In your specific situation, you could send multi rows in one INSERT query, but in many other situations this is not possible because you have different commands (eg LOCK TABLE... SELECT... INSERT... UPDATE... UNLOCK TABLES)

At that point in codeigniter you should user:

mysqli_multi_query($this->db->conn_id, $sql);

I didn't even know the answer, the answer was originally posted as a comment by Kumar (https://stackoverflow.com/users/523794/kumar) at Codeigniter - how to run multiple/batch queries?

Note: You have to set you database driver to mysqli in /application/config/database.php

Hope it helps some one.

Community
  • 1
  • 1
Mohammad Naji
  • 5,372
  • 10
  • 54
  • 79
2

Unfortunately, you need to do something like:

$this->db->query("INSERT INTO `students` (`first_name`,`last_name`) VALUES  ('teejay', 'obazee'),('maev', 'shadowsong'),('jaina', 'proudmore')");

Or use someone else's class to build the query: http://codefury.net/2009/12/insert-multiple-rows-into-a-database-with-codeigniter/

Billiam
  • 2,349
  • 2
  • 19
  • 19