Skip to content

Latest commit

 

History

History
57 lines (42 loc) · 2.03 KB

pdo-begintransaction.md

File metadata and controls

57 lines (42 loc) · 2.03 KB
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

PDO::beginTransaction

[!INCLUDEDriver_PHP_Download]

Turns off auto commit mode and begins a transaction.

Syntax

  
bool PDO::beginTransaction();  

Return Value

true if the method call succeeded, false otherwise.

Remarks

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].

Example

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;  
?>  

See Also

PDO Class

PDO