codeigniter db->delete() returns true always?

后端 未结 2 1393
渐次进展
渐次进展 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';
    }
    

提交回复
热议问题