First, the data engine InnoDB with Begin,rollback,commit to commit the transaction, begin the transaction after the error occurs ROLLBACK TRANSACTION rollback or no error on commit transaction commit confirmation completed.
Start transaction begins with the statement block between the transaction begin and end, set autocommit=0 does not allow the transaction to be automatically committed after setting to end the set autocommit=1.
The following is an example of a data table engine that supports transactional processing.
mysql_query("Start Transaction");mysql_query("Set autocommit=0");mysql_query("Begin"); $sql= "INSERT into student (Name,num) VALUES (' test1 ', ' 0 ')";$sql 2= "INSERT into student (Name,num) VALUES (null, ' 0 ')";//Wrong Writing$res=mysql_query($sql);$res 1=mysql_query($sql 2); if($res&&$res 1){ mysql_query("Commit"); Echo' submitted successfully. <br/> ';}Else{ mysql_query("Rollback"); Echo' Data rollback. <br/> ';}mysql_query("Set Autocommit=1");mysql_query("End");
Second, the table locking method can be used for MyISAM engine databases that do not support transactions:
MyISAM & InnoDB All support, lock tables can lock the table for the current thread. If the table is locked by another thread, it will clog until all locks can be obtained. UNLOCK tables can release any locks held by the current thread. When a thread publishes another lock tables, or when the connection to the server is closed, all tables locked by the current thread are implicitly unlocked.
The following example is an example of using table locks for transactions that do not support commit
mysql_query("Lock tables Student write");//Lock Student Table write can also be read$sql= "INSERT into student (Name,num) VALUES (' Sansheng III ', ' 0 ')";$res=mysql_query($sql);if($res){ Echo' submitted successfully.!<br/> ';}Else{ Echo' Failure!<br/> ';}mysql_query("UNLOCK TABLES");//Release Lock
Complete example of the above code: HTTP://PAN.BAIDU.COM/S/1PLKBKKJ
Password: bh49
Examples of detailed analysis of PHP+MYSQL transaction processing example