CodeIgniter - 如何捕捉数据库错误? [英] CodeIgniter - how to catch DB errors?
本文介绍了CodeIgniter - 如何捕捉数据库错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让CI在遇到DB错误时抛出异常而不是显示如下消息:
Is there a way to make CI throw an exception when it encounters a DB error instead of displaying a message like:
发生数据库错误错误编号:1054'where 子句'中的未知列 'foo' SELECT * FROM (FooBar
) WHERE foo
= '1'
A Database Error Occurred Error Number: 1054 Unknown column 'foo' in 'where clause' SELECT * FROM (
FooBar
) WHEREfoo
= '1'
注意:我只希望这发生在一个控制器中.在其他控制器中,我很高兴它显示 DB 错误消息.
NOTE: I only want this to happen in one controller. In the other controllers, I'm happy for it to display the DB error messages.
推荐答案
试试这些 CI 功能
$this->db->_error_message(); (mysql_error equivalent)
$this->db->_error_number(); (mysql_errno equivalent)
CODEIGNITER 3 的更新
不推荐使用函数,使用 error()
代替:
Functions are deprecated, use error()
instead:
$this->db->error();
这篇关于CodeIgniter - 如何捕捉数据库错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文