php USES transaction of Transaction in PDO

  • 2020-05-07 19:19:00
  • OfStack

And in the process of execution, if one of them on failure to roll back the operation of all has changed. If successful, this series will be permanent and effective operation. 1 transaction good operation is solved by using the database out of sync problem. At the same time, through the transaction to carry out large amount of data, execution efficiency can be improved a lot.

In PDO, transactions are already very simple. The following is a basic example of inserting 1000000 pieces of data into an SQLite database and rolling them back when something goes wrong.
 
try 
{ 
$conn = new PDO('sqlite:Transactioion.s3db'); 
$conn->beginTransaction(); 
for($i=0; $i<1000000; $i++) 
{ 
$conn->exec("insert into [users] values(null,'username')"); 
} 
$conn->commit(); 
} 
catch(PDOException $ex) 
{ 
$conn->rollBack(); 
} 

Related articles: