无法捕获我的 mysql 错误
Can't catch my mysql errors
我在 php 的服务器端使用 codeigniter。
我在我的用户 table 上设置了唯一的电子邮件字段。
问题是无论我尝试什么,我都无法捕获尝试插入重复电子邮件时生成的错误 mysql。
我在我的模型中尝试了什么:
function insert($arr) {
$query= $this->CI->db->insert('user', $arr);
if($query){
return $this->CI->db->insert_id();
} else {
$msg = $this->CI-db->_error_message();
return $msg;
}
}
问题是一切都很好,直到我得到一个副本,而我实际上在 $msg 中什么也没有。我知道调试已从数据库配置文件中打开。
如果您的数据库配置 'db_debug' => TRUE,
您的代码将退出并显示错误消息,您将无法访问此行 $msg = $this->CI-db->_error_message();
。
所以要捕获错误消息,您需要设置。
db_debug' => FALSE
在 CI-2 您的上述代码将起作用。See more at this question
但是在CI-3 那些功能不可用,它会产生php 未定义的方法错误。 CI-3 有一个方法 display_error。你可以看看。
我的解决方案:如果你想要错误,你可以使用这条线
$msg = $this->db->conn_id->error_list;
这将为您提供错误列表 array.But 请记住您需要设置 db_debug' => FALSE
我在 php 的服务器端使用 codeigniter。
我在我的用户 table 上设置了唯一的电子邮件字段。 问题是无论我尝试什么,我都无法捕获尝试插入重复电子邮件时生成的错误 mysql。
我在我的模型中尝试了什么:
function insert($arr) {
$query= $this->CI->db->insert('user', $arr);
if($query){
return $this->CI->db->insert_id();
} else {
$msg = $this->CI-db->_error_message();
return $msg;
}
}
问题是一切都很好,直到我得到一个副本,而我实际上在 $msg 中什么也没有。我知道调试已从数据库配置文件中打开。
如果您的数据库配置 'db_debug' => TRUE,
您的代码将退出并显示错误消息,您将无法访问此行 $msg = $this->CI-db->_error_message();
。
所以要捕获错误消息,您需要设置。
db_debug' => FALSE
在 CI-2 您的上述代码将起作用。See more at this question
但是在CI-3 那些功能不可用,它会产生php 未定义的方法错误。 CI-3 有一个方法 display_error。你可以看看。
我的解决方案:如果你想要错误,你可以使用这条线
$msg = $this->db->conn_id->error_list;
这将为您提供错误列表 array.But 请记住您需要设置 db_debug' => FALSE