Home  >  Article  >  Backend Development  >  Detailed explanation of the method of implementing MySQL transaction processing in PHP

Detailed explanation of the method of implementing MySQL transaction processing in PHP

怪我咯
怪我咯Original
2017-07-12 16:33:512686browse

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 statements

Generally 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:


The code is as follows:

<?PHP 
$LinkID =mysql_connect(&#39;localhost:3307&#39;,&#39;root&#39;,*******); 
mysql_select_db(&#39;web_his&#39;,$LinkID); 
mysql_query("set names utf8"); 
 
/* 创建事务 */ 
mysql_query(&#39;START TRANSACTION&#39;) or exit(mysql_error()); 
$ssql1="insert into pf_item values(&#39;22&#39;,&#39;我们&#39;,&#39;30&#39;)";  //执行sql 1 
if(!mysql_query($ssql1)){ 
   echo $ssql1.mysql_errno().":".mysql_error()."<br>"; 
   mysql_query(&#39;ROLLBACK&#39;) or exit(mysql_error());//判断当执行失败时回滚 
   exit; 
} 
$ssql1="insert into pf_item values(&#39;21&#39;,&#39;hell&#39;,&#39;10&#39;)";  //执行sql 2 
if(!mysql_query($ssql1)){
  echo $ssql1.mysql_errno().":".mysql_error()."<br>"; 
     mysql_query(&#39;ROLLBACK&#39;) or exit(mysql_error());//判断当执行失败时回滚 
   exit; 
} 
 
mysql_query(&#39;COMMIT&#39;) 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn