4

Please Refer to this question I asked
Codeigniter Insert Multiple Rows in SQL

To restate

<tr>
<td><input type="text" name="user[0][name]" value=""></td>
<td><input type="text" name="user[0][address]" value=""><br></td>
<td><input type="text" name="user[0][age]" value=""></td>
<td><input type="text" name="user[0][email]" value=""></td>
</tr>
<tr>
<td><input type="text" name="user[1][name]" value=""></td>
<td><input type="text" name="user[1][address]" value=""><br></td>
<td><input type="text" name="user[1][age]" value=""></td>
<td><input type="text" name="user[1][email]" value=""></td>
</tr>
..........

Can Be Inserted into MySQL as this

 foreach($_POST['user'] as $user)
{
    $this->db->insert('mytable', $user);
}

This results in multiple MySQL queries. Is it possible to optimise it further, so that the insert occurs in one query

Something like this

insert multiple rows via a php array into mysql

but taking advantage of codeigniters simpler syntax. Thanks

1
  • 4
    Unless you have hundreds of thousands of records to INSERT, you won't gain anything and the real bottleneck are the table indexes, and not the round-trip cost of running the query multiple times. Commented Nov 12, 2010 at 15:34

4 Answers 4

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

Check the code igniter user guide for more details.

Sign up to request clarification or add additional context in comments.

Comments

0

CodeIgniter does not seem to have a mutli-insert method. Moreover, even if it had, you may run into issues like:

  • Hitting the maximum query length cap
  • Having to lock the table until inserts are done
  • Dealing with errors can be a bit harder
  • ...

Comments

0

Codeigniter 2 (upcoming release) will have a batch insert method.

1 Comment

thanks, i am already using ci 2, will look up the documentation.
0

In CI 2, there is a set_insert_batch() Active Record function.

1 Comment

No documentation though, and an array I assembled doesn't get inserted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.