PHPpdo cannot be rolled back

Source: Internet
Author: User
PHPpdo transactions cannot roll back publicfunctionsubmit () {try {$ this-& gt; beginTransaction (); $ this-& gt; exec ('updatepeoplesetage = 2 '); $ this-& gt; exec PHP pdo transaction cannot be rolled back
Public function submit (){
Try {
$ This-> beginTransaction ();
$ This-> exec ('update people set age = 2 ');
$ This-> exec ('Insert INTO course (course, num) VALUES ("mathematics", 100 )');
$ This-> commit ();
} Catch (Exception $ e ){
$ This-> rollBack ();
}
}

This is a general example of the transaction module of the younger brother. the database engine is InnoDB, but I don't know why pdo always does not perform rollback if SQL statements are executed incorrectly. please specify it as an expert. thank you very much...

------ Solution --------------------
Do not try it. I don't think exceptions will be thrown here.
PHP code
$ This-> beginTransaction (); $ this-> exec ('update people set age = 2'); // $ step1 = mysql_error (); // in your class, change to get the error code if an SQL execution error occurs. the corresponding function $ this-> exec ('Insert INTO course (course, num) VALUES ("mathematics", 100 )'); // $ step2 = mysql_error (); if (! $ Step1 &&! $ Step2) {$ this-> commit ();} else {$ this-> rollBack ();}
------ Solution --------------------
Agree to the 2nd floor,
The two statements should only return true or false.

You can use if to judge it. do not use try

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.