如何在CodeIgniter PHP中捕获DB错误 [英] How to catch DB errors in CodeIgniter PHP
问题描述
我刚刚接触CodeIgniter,PHP和MySQL。我想处理DB生成的错误。从Stackoverflow中的一个帖子,我知道通过以下语句可以捕获错误。
I am new to CodeIgniter, PHP and MySQL. I want to handle the DB generated errors. From one of the post in Stackoverflow, I knew that by following statement one can catch the error.
$this->db->_error_message();
但我不知道使用它的确切语法。假设我想通过以下语句更新名为table_name的表的记录:
But I cannot figure out the exact syntax of using that. Suppose I want to update the records of table named "table_name" by the following statement:
$array['rank']="8";
$array['class']="XII";
$this->db->where('roll_no',$roll_no);
$this->db->update("table_name", $array);
在上面的代码中,我想要捕获DB错误,只要任何DB级别违例发生,名称无效或发生某些唯一约束违例。如果有人帮助我解决,我会非常感激。谢谢。
Here in the above code I want to catch the DB error whenever any DB level violation occurs i.e. either field name is not valid or some unique constraint violation occurs. If anyone helps me to fix that I would be really grateful. Thank you.
推荐答案
codeIgniter有函数
codeIgniter has functions for it
$this->db->_error_message();
$this->db->_error_number();
if(!$this->db->update("table_name", $array))
{
$this->db->_error_message();
$this->db->_error_number();
}
这篇关于如何在CodeIgniter PHP中捕获DB错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!