Multiple Queries in Codeigniter [duplicate]

十年热恋 提交于 2019-12-22 11:02:54

问题


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.


回答1:


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/




回答2:


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



回答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.



来源:https://stackoverflow.com/questions/2606602/multiple-queries-in-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!