1

in this example.. how can i simplified and fasten up the update and insert of data in the database?

//if count($arr_list['sample_element1'] is 500+++ or more records

$i =0;
while ($i < count($arr_list['sample_element1'])) {

$update_db= array('column1' => $arr_list['sample_element1'][$i],'column2' => $arr_list['sample_element2'][$i]);

$this->db->update('sample_table',$update_db);

$i++;
}

if the record to be save is over 1000 data...it will take about sometime to finish the process.. thank in advance.. =)

1

2 Answers 2

0

use batch_update

$this->db->update(table_name,array,where_key_word);

here table_name = your table name

and array = an array containing multiple associative array

and where_key_word = the column which should be used in where condition

also read this question thread for more information

batch updating thousands of element together may create performance issue.Better split them up and update by smaller batch.Also use transactions so that if any of the update fails the whole update procedure can be rolled back

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

Comments

0

Taking @AL-zami's answer you're looking for this

$data = [];
$this->db->trans_start();
foreach($arr_list as $item)
{
    $data[] = [
        'column1' => $item['element1'],
        'column2' => $item['element2'],
        'column3' => $item['element3']
    ];
}
$this->db->update_batch('mytable', $data, 'column1');
$this->db->trans_complete();

Where 'column1' is the column used for the "WHERE" condition.

2 Comments

yes yes.. i have the same idea... but what if you have two or more where clause? do i have to add another parameter like this? $this->db->update_batch('mytable', $data, 'column1','column2','column3');
No, that won't work. update_batch() won't accept that. Try using another $this->db->where('field', $value) call. But I don't think that will work..

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.