Tutorial on using Transaction _ php in PDO in PHP

Source: Internet
Author: User
In php, transactions are used in PDO ). In addition, if an execution fails during execution, you can roll back all the changed operations. if the operation is successful, the operation will be permanently valid. if one of the transactions fails during execution, you can roll back all the changed operations. if the operation is successful, the operation will be permanently valid. transactions solve the problem of non-synchronization during database operations. at the same time, the execution efficiency can be greatly improved when a transaction is used to execute a large amount of data.

In PDO, the transaction is very simple. the following basic example shows how to insert 1000000 pieces of data into the SQLite database and roll back when an error occurs.

The code is as follows:


Try
{
$ Conn = new PDO ('sqlite: Transactioion. s3db ');
$ Conn-> beginTransaction ();
For ($ I = 0; I I <1000000; $ I ++)
{
$ Conn-> exec ("insert into [users] values (null, 'Username ')");
}
$ Conn-> commit ();
}
Catch (PDOException $ ex)
{
$ Conn-> rollBack ();
}

Rollback. if one of the operations fails, all the changed operations can be rolled back. if the operation is successful, all the operations will be permanently valid. a good solution for the transaction...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.