database transaction in php code example
Example 1: transaction database with php
<?php
try {
$dbh = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2',
array(PDO::ATTR_PERSISTENT => true));
echo "Connecté\n";
} catch (Exception $e) {
die("Impossible de se connecter : " . $e->getMessage());
}
try {
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->beginTransaction();
$dbh->exec("insert into staff (id, first, last) values (23, 'Joe', 'Bloggs')");
$dbh->exec("insert into salarychange (id, amount, changedate)
values (23, 50000, NOW())");
$dbh->commit();
} catch (Exception $e) {
$dbh->rollBack();
echo "Failed: " . $e->getMessage();
}
?>
Example 2: transaction database php
1<?php
$conn->beginTransaction();
try {
...
$conn->beginTransaction();
try {
...
$conn->commit();
} catch (\Exception $e) {
$conn->rollBack();
throw $e;
}
...
$conn->commit();
} catch (\Exception $e) {
$conn->rollBack();
throw $e;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26