简体   繁体   English

Mysqli :: commit()返回false,但没有错误消息

[英]Mysqli::commit() returns false, but no error message

I have three sql queries in a try/catch block. 我在try / catch块中有三个sql查询。 Autocommit is off and all three queries run without errors, I have verified that (it shouldn't reach the commit line if any query fails, but I've verified it via xdebug, stepping through the code one line at a time). 自动提交功能已关闭,所有三个查询都没有错误运行,我已经验证了这一点(如果任何查询失败,它都不会到达提交行,但是我已经通过xdebug验证了它,一次仅一步步执行了代码)。 When the code reached the commit() function call, mysqli returns false, but no error message. 当代码到达commit()函数调用时,mysqli返回false,但没有错误消息。 How can a commit fail when the previous queries all worked? 当先前的查询全部正常工作时,提交如何失败?

This is basically how the code looks: 这基本上是代码的外观:

try{
    $mysqli->autocommit(false);

    $sql1 = "insert into...";
    $mysqli->query($sql); // Works!
    if($mysqli->error)
    {
        $mysqli->rollback();
        throw new Exception....
    }


    $sql2 = "insert into...";
    $mysqli->query($sql); // Works!
    if($mysqli->error)
    {
        $mysqli->rollback();
        throw new Exception....
    }


    $mysqli->commit(); // Fail

}

Check to have: $mysqli->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); 检查是否具有: $mysqli->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); (of course if you want to write/insert in the table) (当然,如果您想在表中写入/插入)

Try to debug following these steps: [ -- start transaction code --] 尝试按照以下步骤调试:[-启动事务代码-]

$q2=$mysqli->query($query2);// query inside transaction $ q2 = $ mysqli-> query($ query2); //事务内查询

print 'q2:'.$mysqli->error; 打印'q2:'。$ mysqli-> error;

[ -- commit /rollback condition / code --------] [-提交/回滚条件/代码--------]

You may receive this error: "Cannot execute statement in a READ ONLY transaction." 您可能会收到以下错误:“无法在只读事务中执行语句。” -> change the parameter of begin transaction to MYSQLI_TRANS_START_READ_WRITE ->将开始交易的参数更改为MYSQLI_TRANS_START_READ_WRITE

Also check to have the innoDB engine set for the tables on which you want to use transactions. 还要检查是否为要使用事务的表设置了innoDB引擎。

You may also test the queries outside the transaction block, to make sure they are error-free. 您也可以在事务块外部测试查询,以确保它们没有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM