title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic |
---|---|---|---|---|---|---|---|
PDO::beginTransaction |
API reference for the PDO::beginTransaction function in the Microsoft PDO_SQLSRV Driver for PHP for SQL Server. |
David-Engel |
v-davidengel |
08/10/2020 |
sql |
connectivity |
reference |
[!INCLUDEDriver_PHP_Download]
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 ends 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 returns to auto commit mode if this method fails.
Support for PDO was added in version 2.0 of the [!INCLUDEssDriverPHP].
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;
?>