codeigniter db->delete() returns true always?

后端 未结 2 1336
渐次进展
渐次进展 2021-01-03 20:16

i have displayed table with record and \"Delete\" image. on delete image click i am deleting the record using ajax. supose there are three records with id 40,41,42 if i dele

相关标签:
2条回答
  • 2021-01-03 20:33

    when we delete from db in codeigniter 2.2.0 using $this->db->delete() we can operate with two flags: 1. $this->db->_error_message() and 2. $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';
    }
    
    0 讨论(0)
  • 2021-01-03 20:50

    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.

    0 讨论(0)
提交回复
热议问题