MySQL transactions are mainly used to process data with large operations and high complexity. For example, in the personnel management system, if you delete a person, you need to delete the basic information of the person, and also delete the information related to the person, such as mailbox, articles, etc. In this way, these Database Operations## The # statement constitutes a transaction!
In MySQL, only databases or tables using the Innodb database engine support transactions. Transaction processing can be used to maintain the integrity of the database and ensure that batches of SQL statements are either all executed or not executed at all. Transactions are used to manage insert, update, delete statementsGenerally speaking, transactions must meet four conditions (ACID): Atomicity (atomicity), Consistency (stability), Isolation (Isolation), Durability (reliability)1. Atomicity of transactions: A set of transactions either succeeds or is withdrawn. 2. Stability: If there is illegal data (foreign key constraints and the like), the transaction will be withdrawn. 3. Isolation: transactions run independently. If the result of one transaction affects other transactions, the other transactions will be withdrawn. 100% isolation of transactions requires sacrificing speed. 4. Reliability: After the software or hardware crashes, the InnoDB data table driver will use the log file to reconstruct and modify it. Reliability and high speed are incompatible. The innodb_flush_log_at_trx_commit option determines when to save transactions to the log. php mysql transaction processing implementation program code is as follows:<?PHP $LinkID =mysql_connect('localhost:3307','root',*******); mysql_select_db('web_his',$LinkID); mysql_query("set names utf8"); /* 创建事务 */ mysql_query('START TRANSACTION') or exit(mysql_error()); $ssql1="insert into pf_item values('22','我们','30')"; //执行sql 1 if(!mysql_query($ssql1)){ echo $ssql1.mysql_errno().":".mysql_error()."<br>"; mysql_query('ROLLBACK') or exit(mysql_error());//判断当执行失败时回滚 exit; } $ssql1="insert into pf_item values('21','hell','10')"; //执行sql 2 if(!mysql_query($ssql1)){ echo $ssql1.mysql_errno().":".mysql_error()."<br>"; mysql_query('ROLLBACK') or exit(mysql_error());//判断当执行失败时回滚 exit; } mysql_query('COMMIT') or exit(mysql_error());//执行事务 mysql_close($LinkID); ?>
The above is the detailed content of Detailed explanation of the method of implementing MySQL transaction processing in PHP. For more information, please follow other related articles on the PHP Chinese website!