4
votes

I'm running update_batch() on a table in CodeIgniter and I'd like to check whether it was successful.

I've tried using affected_rows(), but that only counts the number of form fields that have been modified so it doesn't quite cut it:

$this->db->update_batch("sections", $data, "alias");

log_message("debug", "items in form: ".count($data));
// items in form: 3

log_message("debug", "rows updated: ".$this->db->affected_rows()); 
// rows updated: 0-3 
// depending on whether anything was actually changed on the form

return ($this->db->affected_rows() == count($data)); // unreliable

It seems like a fairly straightforward thing to ask from a batch update function. Is there something I've missed or should I just write my own batch update code?

2
Is using Transactions instead of CI's update_batch an option for you? - Colin Brock
I'm working with MyISAM on this project since it's for a simple CMS which wouldn't get updated very often. Good call though, I might consider switching to InnoDB. - Andrey

2 Answers

4
votes
    $this->db->trans_start();
    $this->db->update_batch($table, $update, $variable);
    $this->db->trans_complete();        
    return ($this->db->trans_status() === FALSE)? FALSE:TRUE;

Hope this helps!. Cheers!

0
votes

Use a simple instruction this will return true or false

return $this->db->update_batch("sections", $data, "alias");