views:

26

answers:

1

Hello,

How can i do batch updates in CodeIgniter instead of firing query each and every time in database?

Thanks in advance :)

+1  A: 

Mysql can do multiple updates or inserts. Usually in an Active Record pattern you do inserts one by one, but for bulk updates or inserts you can do this.

$sql = "INSERT INTO table (id,Col1,Col2) VALUES (1,1,1),(2,2,3),(3,9,3),(4,10,12) ON DUPLICATE KEY UPDATE Col1=VALUES(Col1),Col2=VALUES(Col2);";
$this->db->query($sql);
Elzo Valugi
To add to your comment, you can also use query bindings with that. That way CI will autoescape the values for you. ie. "INSERT INTO table (id,Col1,Col2) VALUES (?,?,?),(?,?,?)" $this->db->query($sql,array(1,1,1,2,2,3));
Johnny Tops