This article mainly introduces the update transaction BUG and solution of the Codeigniter framework. The specific bugs and solutions are described in detail in this article. For more information, see
This article mainly introduces the update transaction BUG and solution of the Codeigniter framework. The specific bugs and solutions are described in detail in this article. For more information, see
Because the ci transaction determines whether the statement is successfully executed if an error is rolled back, even if the number of affected items is 0 during the update operation, the SQL statement execution result is still 1, the execution is successful, but the number of affected items is 0.
The following describes how to solve this problem:
For a transaction that needs to execute many statements at a time
In the update operation, you only need to determine whether the rollback is performed based on whether the number of affected items is 0. The following assumes that the second statement is an update operation.
The Code is as follows:
// Use the manual mode of Codeigniter transactions
$ This-> db-> trans_strict (FALSE );
$ This-> db-> trans_begin ();
$ This-> db-> query ('select... '); // The SELECT operation does not require special processing.
$ This-> db-> query ('insert... '); // Codeigniter automatically handles INSERT errors.
$ This-> db-> query ('Update ...');
If (! $ This-> db-> affacted_rows () {// roll back if the above UPDATE fails.
$ This-> db-> trans_rollback ();
// @ Todo Exception Handling
Exit (); // termination or exit is required to prevent the following SQL code from being executed!
}
$ This-> db-> query ('delete ...');
If (! $ This-> db-> affacted_rows () {// roll back if the DELETE operation fails.
$ This-> db-> trans_rollback ();
// @ Todo Exception Handling
Exit (); // termination or exit is required to prevent the following SQL code from being executed!
}
$ This-> db-> query ('select... '); // The SELECT operation does not require special processing.
$ This-> db-> query ('insert... '); // Codeigniter automatically handles INSERT errors.
If ($ this-> db-> trans_status () === TRUE ){
$ This-> db-> trans_commit ();
} Else {
$ This-> db-> trans_rollback ();
// @ Todo Exception Handling
}
If not many statements are executed at a time, you can make a final judgment to decide the rollback.
If there is no update operation in the statement, you can use automatic transactions.