Definition: PDO
The transaction function is not an additional function of PDO
, but a certain modification of the transaction operations originally supported by MySQL
Encapsulation implementation. Note: Whether the transaction execution is successful is determined by whether the corresponding storage engine of MySQL
supports it.
1. Transaction function review: Transaction refers to changing the default mechanism of writing to the data table in one operation. Instead, it records the operation through the transaction log and writes it in a one-time operation at the end. to the datasheet.
Start transaction: start transaction
, the write operation stops writing directly to the data table, but is recorded to the transaction log
Transaction operation: specific write operation, usually multiple steps and multiple instructions
Commit transaction: that is, the transaction operation ends
Successful submission: commit
, synchronize all transaction log contents to the data table, and clear the current transaction log
Failed rollback: rollback
, directly clear the current transaction log
2. The PDO class provides a set of solutions to implement transaction operations
<?php $pdo = new PDO('mysql:host=localhost;port=3306;dbname=my_database','root','root'); $pdo->beginTransaction() or die('事务开启失败');//开启事务 $pdo->exec('insert into student values()');//执行事务 //终止事务 $pdo->commit(); //成功提交 $pdo->rollback(); //失败回滚
3. In transaction operations, there is a Rollback point
mechanism is not implemented in PDO
. If necessary, it can be implemented through SQL command
settings
<?php $pdo = new PDO('mysql:host=localhost;port=3306;dbname=my_database','root','root'); $pdo->beginTransaction() or die('事务开启失败');//开启事务 $pdo->exec('insert into student values()');//执行事务 //设置回滚点 $pdo->exec('savepoint sp1'); //继续执行事务... //回滚 $pdo->exec('rollback to sp1'); //终止事务 $pdo->commit(); //成功提交 $pdo->rollback(); //失败回滚 ?>
Recommendation: php tutorial,php video tutorial
The above is the detailed content of About PDO transaction function. For more information, please follow other related articles on the PHP Chinese website!