Give an example of Transaction with commit and rollback?

Last updated 5 years, 7 months ago | 730 views 75     5

Tags:- PHP PHP-MySQL

PHP-MySQL | Transaction with commit and rollback

try {
    // First of all, let's begin a transaction
    $db->beginTransaction();

    // A set of queries; if one fails, an exception should be thrown which we are handling at bottom
    $db->query('1 query exe');
    $db->query('2 query exe');
    $db->query('3 query exe');
    
    $db->commit();
} catch (Exception $e) {
    // An exception has been thrown by try
    //This function have all the things which you want to rollback.

    $db->rollback();
}