Sobes.tech
Junior — Middle

How do you implement transaction management in your projects?

sobes.tech AI

Answer from AI

In PHP, transaction management is usually implemented using PDO or mysqli methods. The main idea is to wrap several related database operations into a single transaction to ensure atomicity.

Example with PDO:

try {
    $pdo->beginTransaction();

    // Executing multiple queries
    $pdo->exec("INSERT INTO users (name) VALUES ('Ivan')");
    $pdo->exec("UPDATE accounts SET balance = balance - 100 WHERE user_id = 1");

    $pdo->commit();
} catch (Exception $e) {
    $pdo->rollBack();
    echo "Transaction error: " . $e->getMessage();
}

This approach guarantees that either all operations succeed, or in case of an error, no changes will be saved.

How do you implement transaction management in your… - sobes.tech