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