Export (0) Print
Expand All

PDO::beginTransaction

Turns off auto commit mode and begins a transaction.


bool PDO::beginTransaction();

true if the method call succeeded, false otherwise.

The transaction begun with PDO::beginTransaction will end when PDO::commit or PDO::rollback is called.

PDO::beginTransaction is not affected by (and does not affect) the value of PDO::ATTR_AUTOCOMMIT.

You are not allowed to call PDO::beginTransaction before the previous PDO::beginTransaction is ended with PDO::rollback or PDO::commit.

The connection will return to auto commit mode if this method fails.

Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server.

The following example uses a database called Test and a table called Table1. It starts a transaction and then issues commands to add two rows and then delete one row. The commands are sent to the database and the transaction is explicitly ended with PDO::commit.

<?php
   $conn = new PDO( "sqlsrv:server=(local); Database = Test", "", "");
   $conn->beginTransaction();
   $ret = $conn->exec("insert into Table1(col1, col2) values('a', 'b') ");
   $ret = $conn->exec("insert into Table1(col1, col2) values('a', 'c') ");
   $ret = $conn->exec("delete from Table1 where col1 = 'a' and col2 = 'b'");
   $conn->commit();
   // $conn->rollback();
   echo $ret;
?>

Reference

Other Resources

PDO

Community Additions

Show:
© 2014 Microsoft