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