php codeigniter db->delete() 总是返回 true 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9093334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter db->delete() returns true always?
提问by www.amitpatil.me
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 delete record with ID = 40, responce returns "1" and record gets deleted, next time if i again click on delete image it again returns "1".
我已经显示了带有记录和“删除”图像的表格。在删除图像上单击我正在使用 ajax 删除记录。假设有 id 为 40,41,42 的三个记录,如果我删除 ID = 40 的记录,响应返回“1”并且记录被删除,下次如果我再次单击删除图像,它再次返回“1”。
codeigniters db->delete() method returns "1" always ? do i need to check manually if record exists and then proceed to delete ? below is my code iin ajax.php
codeigniters db->delete() 方法总是返回“1”?我是否需要手动检查记录是否存在然后继续删除?下面是我在 ajax.php 中的代码
$res = $this->db->delete(tbl_user_groups, array('owner_id' => $admin,'user_group_id'=>$gid));
if($res) echo json_encode (array("success"=>"true"));
else echo json_encode (array("success"=>"false"));
回答by Sudhir Bastakoti
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:
该DB-> delete()的返回TRUE
,如果删除操作是成功的。只有FALSE
当它不能删除该行时它才会返回。我认为您应该检查以下内容:
$this->db->affected_rows();
which returns number and not a Boolean, which you can check with your If conditions.
它返回数字而不是布尔值,您可以使用 If 条件进行检查。
回答by ishubin
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()
当我们使用 codeigniter 2.2.0 从 db 中删除时,$this->db->delete()
我们可以使用两个标志进行操作:1.$this->db->_error_message()
和 2.$this->db->affected_rows()
So after db query we get 1 and 2 like:
所以在 db 查询之后,我们得到 1 和 2,如:
DELETED: '', 1
已删除:'', 1
NOT DELETED: '', 0 // row with id not found, but sql completed ok
NOT DELETED: '', 0 // 没有找到 id 的行,但是 sql 完成了
SQL ERROR: string, -1
SQL 错误:字符串,-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';
}