PHP, MySQL, PDO Transactions - Does the code inside try block stop at commit()?

前端 未结 3 1451
一生所求
一生所求 2021-01-23 08:02

I\'m pretty new to transactions.

Before, what I was doing was something like:

Code Block 1

$db = new PDO(...);

$stmt = $db->prepare(         


        
3条回答
  •  不知归路
    2021-01-23 08:41

    The execution is stopped when an exception is thrown.

    The first return will not be reached but the catch statement will be executed.

    You can even return the commit directly:

    $dbh->beginTransaction();
    try {
        // insert/update query
        return $dbh->commit();
    } catch (PDOException $e) {
         $dbh->rollBack();
         return false;
    }
    

提交回复
热议问题