如何捕获CodeIgniter PHP中的DB错误

时间:2021-10-05 07:44:45

I am new to CodeIgniter, PHP and MySQL. I want to handle the DB generated errors. From one of the post in *, I knew that by following statement one can catch the error.

我是CodeIgniter,PHP和MySQL的新手。我想处理数据库生成的错误。从*中的一篇文章中,我知道通过以下语句可以捕获错误。

 $this->db->_error_message();

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:

但我无法弄清楚使用它的确切语法。假设我想通过以下语句更新名为“table_name”的表的记录:

$array['rank']="8";
$array['class']="XII";
$this->db->where('roll_no',$roll_no);
$this->db->update("table_name", $array);

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.

在上面的代码中,我想在发生任何数据库级别违规时捕获数据库错误,即字段名称无效或发生某些唯一约束违规。如果有人帮我解决这个问题,我将非常感激。谢谢。

3 个解决方案

#1


6  

codeIgniter has functions for it

codeIgniter具有它的功能

$this->db->_error_message(); 
$this->db->_error_number(); 


if(!$this->db->update("table_name", $array))
{
    $this->db->_error_message(); 
    $this->db->_error_number(); 
}

#2


5  

You can debug the database error on database configuration in (config/database.php) like this:

您可以在(config / database.php)中调试数据库配置中的数据库错误,如下所示:

$db['default']['db_debug'] = TRUE;

More info read here

更多信息请阅读此处

Also you can use Profiler to see all the queries and their speed. In controller you can put this:

您还可以使用Profiler查看所有查询及其速度。在控制器中你可以把它:

$this->output->enable_profiler(TRUE);

More information read here

更多信息请阅读此处

#3


1  

On Codeigniter version 2,

在Codeigniter版本2上,

$this->db->_error_message(); 
$this->db->_error_number();

On Codeigniter version 3,

在Codeigniter版本3上,

$db_error = $this->db->error();
echo '<pre>';print_r($db_error);echo '</pre>';

#1


6  

codeIgniter has functions for it

codeIgniter具有它的功能

$this->db->_error_message(); 
$this->db->_error_number(); 


if(!$this->db->update("table_name", $array))
{
    $this->db->_error_message(); 
    $this->db->_error_number(); 
}

#2


5  

You can debug the database error on database configuration in (config/database.php) like this:

您可以在(config / database.php)中调试数据库配置中的数据库错误,如下所示:

$db['default']['db_debug'] = TRUE;

More info read here

更多信息请阅读此处

Also you can use Profiler to see all the queries and their speed. In controller you can put this:

您还可以使用Profiler查看所有查询及其速度。在控制器中你可以把它:

$this->output->enable_profiler(TRUE);

More information read here

更多信息请阅读此处

#3


1  

On Codeigniter version 2,

在Codeigniter版本2上,

$this->db->_error_message(); 
$this->db->_error_number();

On Codeigniter version 3,

在Codeigniter版本3上,

$db_error = $this->db->error();
echo '<pre>';print_r($db_error);echo '</pre>';