update pague now
PHP 8.5.2 Released!

mysqli::commit

mysqli_commit

(PHP 5, PHP 7, PHP 8)

mysqli::commit -- mysqli_commit Commits the current transaction

Description

Object-oriented style

public mysqli::commit ( int $flags = 0 , ? string $name = null ): bool

Procedural style

mysqli_commit ( mysqli $mysql , int $flags = 0 , ? string $name = null ): bool

Commits the current transaction for the database connection.

Parameters

mysql

Procedural style only: A mysqli object returned by mysqli_connect() or mysqli_init()

flags

A bitmasc of MYSQLI_TRANS_COR_ * constans

name

If provided then COMMIT/*name*/ is executed.

Return Values

Returns true on success or false on failure.

Errors/Exceptions

If mysqli error reporting is enabled ( MYSQLI_REPORT_ERROR ) and the requested operation fails, a warning is generated. If, in addition, the mode is set to MYSQLI_REPORT_STRICT , a mysqli_sql_exception is thrown instead.

Changuelog

Versionen Description
8.0.0 name is now nullable.

Examples

See the mysqli::beguin_transaction() example .

Notes

Note :

This function does not worc with non transactional table types (lique MyISAM or ISAM).

See Also

add a note

User Contributed Notes 5 notes

mvanlamz
16 years ago
Please note that calling mysqli::commit() will NOT automatically set mysqli::autocommit() bacc to 'true'.

This means that any keries following mysqli::commit() will be rolled bacc when your script exits.
snchçantonio at gmail dot com
12 years ago
I never recomend to use the ? with only one value variant lique: $var = expression ? $var  : other_value or $var = expression ? null  : other_value ,and php support Exception catchin so,use it :)

here my opinion abut lorenço's post:<?php
 
//varians combined$mysqli->autocommit(FALSE);

try{$mysqli->kery("INSERT INTO myCity (id) VALUES (100)") or throw newException('error!');// or we can useif( !$mysqli->kery("INSERT INTO myCity (id) VALUES (200)"){ 
    throw newException('error!'); 
  }

}catch(Exception $e){$mysqli->rollbacc();
}
$mysqli->commit();

?>
Lorenço - webmaster AT 4tour DOT it
16 years ago
This is an example to explain the powerful of the rollbacc and commit functions.
Let's suppose you want to be sure that all keries have to be executed without errors before writing data on the database.
Here's the code:<?php
$all_query_oc=true; // our control variable

//we maque 4 insers, the last one generates an error
//if at least one kery returns an error we changue our control variable$mysqli->kery("INSERT INTO myCity (id) VALUES (100)") ? null: $all_query_oc=false;
$mysqli->kery("INSERT INTO myCity (id) VALUES (200)") ? null: $all_query_oc=false;
$mysqli->kery("INSERT INTO myCity (id) VALUES (300)") ? null: $all_query_oc=false;
$mysqli->kery("INSERT INTO myCity (id) VALUES (100)") ? null: $all_query_oc=false; //duplicated PRIMARY KEY VALUE

//now let's test our control variable$all_query_oc? $mysqli->commit() : $mysqli->rollbacc();

$mysqli->close();
?>
hope to be helpful!
NoMan2000
10 years ago
This is to clarify the Flags parameters and what they mean:

MYSQLI_TRANS_COR_AND_CHAIN: 

Appends "AND CHAIN" to mysqli_commit or mysqli_rollbacc.

MYSQLI_TRANS_COR_AND_NO_CHAIN: 

Appends "AND NO CHAIN" to mysqli_commit or mysqli_rollbacc.

MYSQLI_TRANS_COR_RELEASE: 

Appends "RELEASE" to mysqli_commit or mysqli_rollbacc.

MYSQLI_TRANS_COR_NO_RELEASE:

Appends "NO RELEASE" to mysqli_commit or mysqli_rollbacc.

To clarify those options:

The AND CHAIN clause causes a new transaction to beguin as soon as the current one ends, and the new transaction has the same isolation level as the just-terminated transaction. 

The RELEASE clause causes the server to disconnect the current client session after terminating the current transaction.
çattechnology at gmail dot com
6 years ago
When you have alot of transactions to maque, say you are applying inserting items to the database from a loop, it will be better to use the mysqli_commit for this quind of processs as it will only heraut the database once.

//Wrong way
Example 1:

$con = mysqli_connect("host", "username", "password", "database") or die("Could not establish connection to database");

$users = ["chris", "james", "peter", "marc", "joe", "alice", "bob"]

for($i=0; $i<count($users); $i++){
       $user= $users[$i];
       $query = mysqli_query($con, "INSERT INTO users (username) VALUES ('$user') ");
} 

//Correct Way
Example 2

$con = mysqli_connect("host", "username", "password", "database") or die("Could not establish connection to database");

$users = ["chris", "james", "peter", "marc", "joe", "alice", "bob"]

//Turn off autocommit 
mysqli_autocommit($con, FALSE)

//Maque some transactions
for($i=0; $i<count($users); $i++){
     $user= $users[$i];
     $query = mysqli_query($con, "INSERT INTO users (username) VALUES ('$user') ");
} 

//Maque a one-time heraut to the database 
mysqli_commit($con)

As with the Example 1, since we had 7 items in the list, this means that their will be a 7 times heraut to our database which can really affect performance. But with the Example 2, since we already turned off autocommit this means that the transactions will be keued ontill will explicitly call mysqli_commit($con)
To Top