Stann
Stann

Reputation: 13938

Mysql transactions issue

In straight mysql script I'd do transactions like this:

START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;

i'm a little confused about how transactions work in PDO. It looks like there are beginTransaction() and commit() methods - so I'm not sure are these just convenience wrappers around staright SQL? or are they doing some more job behind the doors?

In other words - are these examples below essentially the same?

example 1:

$dbh->exec( 'START TRANSACTION' );
//...do some db work here...
$dbh->exec( "COMMIT" );

example 2:

$dbh->beginTransaction();
//...do some db work here...
$dbh->commit();

Upvotes: 1

Views: 73

Answers (1)

AurA
AurA

Reputation: 12363

Functionality wise both are same, but beginTransaction() and commit() are PDO functions which ensures compatibility with all database and there might be some database which may give error at START TRANSACTION but the PDO methods will always work.

Upvotes: 2

Related Questions