开发者

Multiple Queries in Codeigniter [duplicate]

开发者 https://www.devze.com 2022-12-26 00:16 出处:网络
This question already has answers here: Closed 9 years ago. Possible Duplicate: MYSQL multiple insert in codeigniter
This question already has answers here: Closed 9 years ago.

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 student开发者_如何转开发s (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.


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');


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.


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消