Home > Backend Development > PHP Tutorial > Using Transaction in PDO in PHP_PHP Tutorial

Using Transaction in PDO in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:29:42
Original
1010 people have browsed it

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 into a SQLite database 1,000,000 pieces of data, and rollback when an error occurs.

Copy 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();
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323341.htmlTechArticleAnd 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. The transaction is well solved...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template