交易不返回虚假信息

transaction not returning false msg

我正在尝试使用事务更新 3 tables,如果任何一个失败,我需要回滚所有 tables。对于一个 table 外键约束失败,但它不会 return false 语句而是显示数据库的控制台错误。回滚正在工作。我在下面包含我的代码。请帮忙。 下面的函数帮助我执行一些查询

    函数 funcname($val1,$val2,$val3){
            $this->db->trans_start();//开始交易
            try { // 尝试执行查询
                $this->db->query("UPDATE tab1 SET name = 1 WHERE id=".$val1);
                $this->db->query("UPDATE school SET emp = 2 WHERE id=".$val2);
                $this->db->query("UPDATE profile SET status = 4 WHERE id=".$val3);
                $this->db->trans_complete();
                return 正确;
            } catch (Exception $ex) { //异常回滚到初始状态
                $this->db->trans_rollback();
                return 错误;
            }
}

如果您正在使用 trans_start()trans_complete() 那么您不需要使用 try catch 语句。你的功能将是这样的。

function funcname($val1,$val2,$val3){
        $this->db->trans_start();//starting transaction
        $this->db->query("UPDATE tab1 SET name = 1 WHERE id=".$val1);
        $this->db->query("UPDATE school SET emp = 2 WHERE id=".$val2);
        $this->db->query("UPDATE profile SET status = 4 WHERE id=".$val3);
        $this->db->trans_complete();
        if ($this->db->trans_status() === FALSE)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

如果您需要手动执行,请使用下面的代码

function funcname($val1,$val2,$val3){
        $this->db->trans_begin();//starting transaction
        $this->db->query("UPDATE tab1 SET name = 1 WHERE id=".$val1);
        $this->db->query("UPDATE school SET emp = 2 WHERE id=".$val2);
        $this->db->query("UPDATE profile SET status = 4 WHERE id=".$val3);
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return FALSE;
        }
        else
        {
            $this->db->trans_commit();
            return TRUE;
        }
    }

更多参考Transactions : CodeIgniter User Guide

我不知道你的数据库 class 是如何工作的,但可能查询 return false 失败不会抛出异常,所以 trans_complete() return false 也是,然后是你的函数 return TRUE