, , (-):
try {
$db->beginTransaction();
$db->query('first query');
$db->query('second query');
$db->query('third query');
$db->commit();
} catch (Exception $e) {
$db->rollback();
}
, , , Exception:
PDO , , . PDO:: setAttribute PDO:: ATTR_ERRMODE PDO:: ERRMODE_EXCEPTION , - API, , , , , .
, : - : , .
For example, quite often you will have several requests before a transaction (before the start) and another pair of requests after a transaction (after a commit or rollback); and you want these requests to be executed regardless of what happened (or not) in the transaction.
==================================================== ===
You can also use mysqli to achieve the same
For ex
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_autocommit($link, FALSE);
mysqli_query($link, "CREATE TABLE myCity LIKE City");
mysqli_query($link, "ALTER TABLE myCity Type=InnoDB");
mysqli_query($link, "INSERT INTO myCity SELECT * FROM City LIMIT 50");
mysqli_commit($link);
shail source
share