codeigniter db->delete() returns true always? codeigniter db->delete() returns true always? codeigniter codeigniter

codeigniter db->delete() returns true always?


The db->delete() returns TRUE, if delete operation is successful. It would only return FALSE if it COULDN’T delete the row. I think you should be checking something like:

$this->db->affected_rows();

which returns number and not a Boolean, which you can check with your If conditions.


when we delete from db in codeigniter 2.2.0using $this->db->delete() we can operate with two flags:1. $this->db->_error_message()and2. $this->db->affected_rows()

So after db query we get 1 and 2 like:

DELETED: '', 1

NOT DELETED: '', 0 // row with id not found, but sql completed ok

SQL ERROR: string, -1

My choice is the following check:

$this->db->delete($this->table,array('id'=>$id));if ($this->db->_error_message()) {    $result = 'Error! ['.$this->db->_error_message().']';} else if (!$this->db->affected_rows()) {    $result = 'Error! ID ['.$id.'] not found';} else {    $result = 'Success';}