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.
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 */
?>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
