PDO::commit – Commit a Transaction in PHP

The article explains how PDO::commit() finalizes a database transaction in PHP, describes its parameterless signature, return values, and provides a complete code example demonstrating starting a transaction, inserting multiple rows, and committing the changes.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PDO::commit – Commit a Transaction in PHP

PDO::commit() commits the current database transaction, returning the connection to autocommit mode until the next call to PDO::beginTransaction().

Parameters

This function takes no parameters.

Return value

Returns TRUE on success or FALSE on failure.

Example

<?php
/* Start a transaction, disable autocommit */
$dbh->beginTransaction();

/* Insert multiple rows atomically */
$sql = 'INSERT INTO fruit (name, colour, calories) VALUES (?, ?, ?)';
$sth = $dbh->prepare($sql);
foreach ($fruits as $fruit) {
    $sth->execute(array($fruit->name, $fruit->colour, $fruit->calories));
}

/* Commit the changes */
$dbh->commit();
/* Now the connection returns to autocommit mode */
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.