Home > Article > Backend Development > Using transaction Transaction in PDO in transactional php
And during the execution process, if one of the execution fails, all changed operations can be rolled back. If the execution is successful, then this series of operations will be permanently effective. Transactions solve the problem of being out of sync when operating the database problem. At the same time, when executing large amounts of data through transactions, the execution efficiency can be improved a lot.
In PDO, transactions are already very simple. The following is a basic example that demonstrates inserting 1,000,000 pieces of data into a SQLite database, and Roll back when an error occurs.
Copy the code The code is as follows:
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();
}
The above introduces the use of transaction Transaction in PDO in transactional php, including transactional aspects. I hope it will be helpful to friends who are interested in PHP tutorials.