Codeigniter处理数据库错误

时间:2016-12-20 23:11:18

标签: php mysql codeigniter

我正在使用codeigniter开发一个网站。我的问题是,

在开发环境中(db_debug = TRUE)。它会抛出错误,如果我使用主键中的现有值提供数据。

我想要做的是,当查询因以下原因失败时:重复外键违规等,在开发环境中而不是显示错误,我想从模型中返回result: truemessage: query failed for some reason

如何实现这一目标?

我正在测试以下代码,

$test_query = $this->db->query("INSERT INTO test SET id = 1, email = 'test@gmail.com'");

$out = array();

if($test_query) {
    // do other queries and get data
    $out['data'] = 'some data from other subsequent database query';
    $out['msg'] = 'query failed for some reason';
    $out['result'] = true;
}
else {   
    $out['result'] = false;
    // fails
}
return $out;

注意:我想仅跳过预测/主要发生/基本错误,例如重复外键违规主键违规等即使在开发模式下,在开发环境中也要注意可能的错误。

我无法使用db_debug = FALSE,因为我仍然想知道其他可能与数据库相关的查询失败,以便我可以在开发应用程序时修复这些错误。我想跳过指定错误的原因是,我不想用这些可预测的错误打破系统流程。

0 个答案:

没有答案
相关问题