This article mainly introduces examples of transaction usage in PHP. This article gives the simplest entry-level example. Friends who need it can refer to it. Next
?
10 11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<🎜>//Database connection<🎜> <🎜>$conn = mysql_connect('localhost', 'root', '');<🎜> <🎜>mysql_select_db('test', $conn);<🎜> <🎜>mysql_query("SET NAMES GBK");<🎜> <🎜> <🎜> <🎜>/*<🎜> <🎜>Tables that support transactions must be of InnoDB type<🎜> <🎜>Can only appear once in a transaction:<🎜> <🎜>mysql_query('START TRANSACTION');//Start transaction<🎜> <🎜>mysql_query(' ROLLBACK ');//Rollback transaction<🎜> <🎜>mysql_query('COMMIT');//Submit transaction<🎜> <🎜> <🎜> <🎜>If multiple rollback transactions occur in a transaction, when the transaction is submitted, only all operations on the database from before the first rollback to after the start of the transaction will be cancelled. All operations after the first rollback to before the transaction is committed will be cancelled. The database operation will still be valid, so generally put the rollback statement only before the commit transaction statement <🎜> <🎜>If a transaction does not have a commit statement, all operations on the database below will be executed from the beginning of the transaction (the execution method returns true or false), but will have no impact on the database. However, when the next transaction statement is executed, the previous transaction will be automatically committed. <🎜> <🎜>*/<🎜> <🎜>mysql_query('START TRANSACTION');<🎜> <🎜>$isBad = 0;<🎜> <🎜> <🎜> <🎜>$ins_testTable1 = "INSERT INTO testtable1(NAME,age)VALUES('first',23)";<🎜> <🎜>if(!mysql_query($ins_testTable1)){<🎜> <🎜>$isBad =1;<🎜> <🎜>}<🎜> <🎜>//The insert statement field name is wrong<🎜> <🎜>$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);<🎜> <🎜>?> |