CodeIgniter捕获数据库错误消息(如果dbdebug为false,则返回0)

时间:2018-01-01 16:34:34

标签: php mysql codeigniter

执行此查询时:

public function delete_user_test($id){
        $this->db->trans_begin();
        $this->db->where('id',$id);
        $this->db->delete($this->user_table);
        $person_id = $this->get_user_account($id)['person_id'];
        $this->db->where('id',$person_id);
        $this->db->delete($this->person_table);
        $this->db->trans_complete();
        if($this->db->trans_status()===false){
            $this->db->trans_rollback();
            return $this->db->error();
        }else{
            $this->db->trans_commit();
            return true;
        }
    }

并且数据库配置文件中的dbdebug设置为true,我将收到全屏错误说

错误号码:1451

无法删除或更新父行:外键约束失败(database_tableacc_codes,CONSTRAINT acc_codes_fk FOREIGN KEY(user_id)参考user_accountsid))

user_accounts删除id =' 2'

文件名:models / AccMdl.php

行号:655

如何从该页面获取错误消息并将其传回横幅而不是整页?如果我将dbdebug变为false,那么我从$ this-> db-> error()得到的所有内容;是0。

我试过四处寻找,但似乎没有人有同样的问题。希望有人可以提供帮助。

编辑:添加了我的数据库配置内容。希望其相关

$db['default'] = array(
    'dsn'   => '',
    'hostname' => host,
    'username' => uname,
    'password' => pw,
    'database' => name,
    'dbdriver' => drvr,
    'dbprefix' => '',
    'pconnect' => FALSE,
//  'db_debug' => (ENVIRONMENT !== 'production'),
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

EDIT2:我设法解决了错误。我使用的方法是删除trans_begin和其他事务项。有没有办法将它与交易项目整合?

$this->db->where('id',$id);
        if(!$this->db->delete($this->user_table)){
            return $this->db->error();
        }

这将产生错误消息。但是我该如何将其与其他人结合起来?

3 个答案:

答案 0 :(得分:0)

$this->db->error()返回一个包含错误的数组'和'代码'你可以检查的元素。

https://www.codeigniter.com/userguide3/database/queries.html#handling-errors

类似于:

if($this->db->trans_status()===false){
    $this->db->trans_rollback();

    // Get the error array
    $error = $this->db->error();
    // Return the message - but that doesn't work with returning true/false.
    return $error('error');

} else { ...

答案 1 :(得分:0)

首先,如果您尝试手动管理交易,请不要使用$this->db->trans_complete()。如果出现问题,它会调用rollback()

其次,不要尝试手动管理交易。我见过的每个案例都有人编写CodeIgniter已经拥有的相同代码 - 但通常不是那么干净。为什么重新发明轮子?

我建议对自动交易有信心。

function delete_user_test($id)
{
    $this->db->trans_start();
    $this->db
        ->where('id', $id)
        ->delete($this->user_table);

    $person_id = $this->get_user_account($id)['person_id'];  //huh???

    $this->db
        ->where('id', $person_id)
        ->delete($this->person_table);

    $this->db->trans_complete();

    if($this->db->trans_status() === false)
    {
        // generate an error... 
        // or use the log_message() function to log your error
    }
}

如果$this->db->error();,您可以尝试使用trans_status() === false,但要知道它只会报告上一次操作的结果。 (使用'mysqli'驱动程序)所以使用这个

$error = this->db->error();  

$error['code']的值可能为0,因为最后一次数据库操作(回滚)可能已成功。

答案 2 :(得分:0)

我设法解决了这个问题。

所以我发现$ this-> db-> error()仅在查询本身之后立即工作,而不在trans_status区域中工作。我找到的解决方法是

public function delete_user_test($id){
        $this->db->trans_begin();
        $this->db->where('id',$id);
        $result = $this->db->delete($this->user_table);
        if(!$result){return $this->db->error();}
        $person_id = $this->get_user_account($id)['person_id'];
        $this->db->where('id',$person_id);
        $result = $this->db->delete($this->person_table);
        if(!$result){return $this->db->error();}
        $this->db->trans_complete();
        if($this->db->trans_status()===false){
            $this->db->trans_rollback();
        }else{
            $this->db->trans_commit();
            return true;
        }
    }

我保留回滚以防万一,但这对我有用