Junior — Middle
Describe your experience with transactional mechanisms in projects.
sobes.tech AI
Answer from AI
In PHP projects, I used transactions mainly when working with databases to ensure data integrity. For example, when performing several related operations (inserting an order and updating stock levels), I wrapped these actions in a transaction so that in case of an error, all changes are rolled back.
Example using PDO:
try {
$pdo->beginTransaction();
$pdo->exec("INSERT INTO orders (user_id, total) VALUES (1, 100)");
$pdo->exec("UPDATE products SET stock = stock - 1 WHERE id = 10");
$pdo->commit();
} catch (Exception $e) {
$pdo->rollBack();
echo "Error: " . $e->getMessage();
}
This approach helps avoid partial data updates and maintains database consistency.