Update transaction BUG and solution of Codeigniter framework

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.