Database connection $conn = mysql_connect (' localhost ', ' root ', '); mysql_select_db (' Test ', $conn); mysql_query ("SET NAMES GBK"); /* The table that supports transactions must be of type InnoDB Can only occur once in a transaction: mysql_query (' Start TRANSACTION ');//Start transaction mysql_query (' ROLLBACK ');//ROLLBACK TRANSACTION mysql_query (' commit ');//COMMIT Transaction If the transaction is rolled back multiple times in a transaction, when the transaction is committed, all operations on the database are canceled after the first rollback until the start of the transaction, and after the first rollback until the transaction is committed, all the database operations are still valid, so the rollback statement is typically placed only before committing the transaction statement If there is no commit statement for a transaction, all of the following database operations are executed from the start of the transaction (the execution method returns the wrong error), but there is no impact on the database, but the previous transaction is automatically committed when the next segment starts the transaction statement */ mysql_query (' START TRANSACTION '); $isBad = 0; $ins _testtable1 = "INSERT into Testtable1 (name,age) VALUES (' first ', 23)"; if (!mysql_query ($ins _testtable1)) { $isBad = 1; } Error inserting statement field name $ins _testtable2 = "INSERT into Testtable1 (name,ages) VALUES (' second ', ' 24 ')"; if (!mysql_query ($ins _testtable2)) { $isBad = 1; } if ($isBad = = 1) { Echo $isBad; mysql_query (' ROLLBACK '); } mysql_query (' COMMIT '); Mysql_close ($conn); ?> |