Transactions (Transaction) is an important feature in the operational database that allows you to book a single, or a series of SQL statements, and then execute them together.
During execution, if one of the executions fails, you can roll back all the changed operations. If the execution succeeds, the sequence of operations will be permanently valid. The transaction solves the problem of being out of sync when manipulating the database. At the same time, the execution efficiency can be much higher when the transaction executes the large amount of data.
In PDO, the transaction has become very simple. The following basic example demonstrates inserting 1 million data into the SQLite database and rolling back when an error occurs.
Try { $conn=NewPDO (' 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 ();}
How to use PDO transactions in PHP