Another important feature of PDO is that it can undo or apply the queries, which are expressed as transaction
, when desired.
Before using the query expressions, we need to state that we started writing queries that we can undo by calling the beginTransaction
method.
After running the BeginTransaction
method, all the add, edit and delete queries we write continue to work as we normally use them. If we want to retrieve all queries in this range while the queries continue, we call the rollBack
method. When we run this method, all add, edit and delete queries we have written from beginTransaction
to the rollBack
method are undone.
An important issue to know when using these expressions; Queries that delete tables completely or add new tables, such as DROP TABLE
or CREATE TABLE
, cannot be undone. However, you can undo any query you delete with DELETE
query or edit with UPDATE
.
<?php
// We express that we have initiated transactions
$db->beginTransaction();
$db->exec('INSERT INTO users (name) VALUES ("Baransel")');
$db->exec('UPDATE users SET name = "Arslan"');
$db->exec('DELETE FROM users WHERE name = "Batuhan"');
// We got back the queries we made
$db->rollBack();
?>