MySQLi Prepared Statements and Transactions

前端 未结 1 1909
后悔当初
后悔当初 2021-02-06 02:10

Is there a way to do transactions with prepared statements?

I mean can I use the following example with $mysqli->autocommit(FALSE); and $mysqli->

相关标签:
1条回答
  • 2021-02-06 02:54

    Prepared statements and transactions are unrelated techniques and technologies.

    You may wish to issue the START TRANSACTION and COMMIT/ROLLBACK commands directly instead of using the dedicated methods. They are functionally equivalent.

    For your loop, you'd issue the START TRANSACTION before your prepare, then your COMMIT after the loop exits. You probably should not try to open a transaction after a prepared statement has been started but before it's been executed.

    For some reason, they didn't add a "start transaction" command in favor of turning off autocommit. It's one of those weird things about mysqli that makes me always recommend PDO instead. :) Opening a transaction implicitly turns off autocommit for the duration of the transaction.

    0 讨论(0)
提交回复
热议问题