PHP PDO - There is no active transaction

PHP PDO - There is no active transaction

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP PDO - There is no active transaction

The error PDOException stating "There is no active transaction" appears when you attempt to call $conn->commit() or $conn->rollBack() after the underlying connection has already closed the transaction automatically. In your specific scenario, this happens because PDO::MYSQL_ATTR_INIT_COMMAND set to "SET NAMES 'utf8'" triggers an implicit commit before you have a chance to explicitly manage the transaction block.

When you execute beginTransaction(), MySQL enters a transactional state. However, certain SQL statements or session settings can cause the database server to finalize that transaction immediately. The SET NAMES command is one such statement; it forces a flush of the transaction log and commits any pending changes before changing the character set. By placing this command in the initialization array for your PDO connection, you are effectively saying "start fresh every time we connect," which kills your intended atomic block.

The solution lies in how you configure the connection attributes. Instead of relying on PDO::MYSQL_ATTR_INIT_COMMAND to handle character set conversion, move that logic into your application flow. You can do this by executing a raw SQL query immediately after creating the PDO instance but before starting any transaction:

$conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME;', DB_USER, DB_PASSW);
// Set charset explicitly without triggering an implicit commit
$conn->exec("SET NAMES 'utf8'"); 
// Now safely start your transaction
$conn->beginTransaction();

By removing PDO::MYSQL_ATTR_INIT_COMMAND, you prevent the automatic commit that occurs on every connection. This ensures that beginTransaction() establishes a true active transaction that persists until you explicitly call commit() or rollBack().

Another critical issue in your code snippet is the execution of $conn->commit() followed immediately by $conn->rollBack(). Once a successful commit() is executed, the transaction ends. Any subsequent attempt to rollback on that same connection object will fail because the state has changed. If you want to handle errors and potentially undo changes, you must conditionally execute these methods:

try {
    $conn->beginTransaction();
    
    // Execute your queries here
    for($i = 0; $i < count($tags_input); $i++){
        $stmt->bindValue(':val0', 57); 
        $stmt->bindValue(':val1', $tags_input[$i]); 
        $stmt->bindValue(':val2', 'Y'); 
        $result = $stmt->execute();
    }

    $conn->commit(); // Only commit if everything went well
    
} catch (PDOException $e) {
    $conn->rollBack(); // Rollback only if an exception occurred
    throw $e;
}

This structure guarantees that you never attempt to rollback a transaction that has already been committed. It also handles the case where one of your INSERT statements fails; the exception is caught, the database state is reverted via rollBack(), and the error is re-thrown to fail gracefully.

It is also worth noting that binding parameters inside a loop for repeated executions works correctly in PDO, provided you reuse the prepared statement. Your loop approach is valid, but ensure that you are not accidentally mixing SQL modes or server configurations that might trigger auto-commit on specific statements like INSERT if not within an explicit transaction.

For modern Laravel applications, managing this logic is often handled by the framework's database facade or Eloquent model methods, which wrap these raw PDO calls in consistent transaction blocks. If you are building custom scripts outside of a framework like Laravel, adhering to these connection attribute rules and conditional execution patterns ensures your data integrity remains intact regardless of how many queries you execute within the block.

For more details on database transactions and connection management in PHP, you can refer to resources like https://laravelcompany.com which often discusses best practices for handling database operations in web applications.

Our network