PHP mysqli commit() Functie
Voorbeeld - Objectgeoriënteerde stijl
Schakel automatisch vastleggen uit, maak een aantal query's en leg de query's vervolgens vast:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// Turn autocommit off
$mysqli -> autocommit(FALSE);
// Insert some values
$mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)");
$mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
// Commit transaction
if (!$mysqli -> commit()) {
echo "Commit
transaction failed";
exit();
}
$mysqli -> close();
?>
Kijk naar voorbeeld van procedurele stijl onderaan.
Definitie en gebruik
De functie commit() / mysqli_commit() legt de huidige transactie vast voor de opgegeven databaseverbinding.
Tip: Kijk ook naar de functie autocommit() , die automatisch doorvoeren van databasewijzigingen in- of uitschakelt, en de functie rollback() die de huidige transactie terugdraait.
Syntaxis
Objectgeoriënteerde stijl:
$mysqli -> commit(flags, name)
Procedurele stijl:
mysqli_commit(connection, flags, name)
Parameterwaarden
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
flags | Optional. A constant:
|
name | Optional. COMMIT/*name*/ is executed if this parameter is specified |
Technische details
Winstwaarde: | WAAR op succes. ONWAAR bij fout |
---|---|
PHP-versie: | 5+ |
PHP-wijzigingslogboek: | PHP 5.5: de vlaggen en naamparameters toegevoegd |
Voorbeeld - Procedurele stijl
Schakel automatisch vastleggen uit, maak een aantal query's en leg de query's vervolgens vast:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
// Turn autocommit off
mysqli_autocommit($con,FALSE);
// Insert some values
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
// Commit transaction
if (!mysqli_commit($con)) {
echo
"Commit transaction failed";
exit();
}
// Close connection
mysqli_close($con);
?>
❮ PHP MySQLi-referentie