mysqli::commit

mysqli_commit

(PHP 5)

mysqli::commit -- mysqli_commitCommits the current transaction

Description

Object oriented style

bool mysqli::commit ([ int $flags [, string $name ]] )

Procedural style

bool mysqli_commit ( mysqli $link [, int $flags [, string $name ]] )

Commits the current transaction for the database connection.

Parameters

link

Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()

flags

A bitmask of MYSQLI_TRANS_COR_* constants.

name

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

Return Values

Returns TRUE on success or FALSE on failure.

Changelog

Version Description
5.5.0 Added flags and name parameters.

Examples

Example #1 mysqli::commit() example

Object oriented style

<?php
$mysqli 
= new mysqli("localhost""my_user""my_password""world");

/* check connection */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE Language LIKE CountryLanguage");

/* set autocommit to off */
$mysqli->autocommit(FALSE);

/* Insert some values */
$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)");
$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)");

/* commit transaction */
if (!$mysqli->commit()) {
    print(
"Transaction commit failed\n");
    exit();
}

/* drop table */
$mysqli->query("DROP TABLE Language");

/* close connection */
$mysqli->close();
?>

Procedural style

<?php
$link 
mysqli_connect("localhost""my_user""my_password""test");

/* check connection */
if (!$link) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

/* set autocommit to off */
mysqli_autocommit($linkFALSE);

mysqli_query($link"CREATE TABLE Language LIKE CountryLanguage");

/* Insert some values */
mysqli_query($link"INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)");
mysqli_query($link"INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)");

/* commit transaction */
if (!mysqli_commit($link)) {
    print(
"Transaction commit failed\n");
    exit();
}

/* close connection */
mysqli_close($link);
?>

See Also

Коментарии

This is an example to explain the powerful of the rollback and commit functions.
Let's suppose you want to be sure that all queries have to be executed without errors before writing data on the database.
Here's the code:

<?php
$all_query_ok
=true// our control variable

//we make 4 inserts, the last one generates an error
//if at least one query returns an error we change our control variable
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (200)") ? null $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (300)") ? null $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null $all_query_ok=false//duplicated PRIMARY KEY VALUE

//now let's test our control variable
$all_query_ok $mysqli->commit() : $mysqli->rollback();

$mysqli->close();
?>

hope to be helpful!
2009-02-11 05:12:53
http://php5.kiev.ua/manual/ru/mysqli.commit.html
Автор:
Please note that calling mysqli::commit() will NOT automatically set mysqli::autocommit() back to 'true'.

This means that any queries following mysqli::commit() will be rolled back when your script exits.
2009-03-31 14:36:59
http://php5.kiev.ua/manual/ru/mysqli.commit.html
I never recomend to use the ? with only one value variant like: $var = expression ? $var  : other_value or $var = expression ? null  : other_value ,and php suport Exception catchin so,use it :)

here my opinion abut lorenzo's post:

  <?php
 
//variants combined

$mysqli->autocommit(FALSE);

try{

 
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") or throw new Exception('error!');

// or we can use

 
if( !$mysqli->query("INSERT INTO myCity (id) VALUES (200)"){ 
    throw new 
Exception('error!'); 
  }

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

?>
2013-08-16 20:38:35
http://php5.kiev.ua/manual/ru/mysqli.commit.html
Автор:
This is to clarify the Flags parameters and what they mean:

MYSQLI_TRANS_COR_AND_CHAIN: 

Appends "AND CHAIN" to mysqli_commit or mysqli_rollback.

MYSQLI_TRANS_COR_AND_NO_CHAIN: 

Appends "AND NO CHAIN" to mysqli_commit or mysqli_rollback.

MYSQLI_TRANS_COR_RELEASE: 

Appends "RELEASE" to mysqli_commit or mysqli_rollback.

MYSQLI_TRANS_COR_NO_RELEASE:

Appends "NO RELEASE" to mysqli_commit or mysqli_rollback.

To clarify those options:

The AND CHAIN clause causes a new transaction to begin 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.
2015-05-27 17:51:00
http://php5.kiev.ua/manual/ru/mysqli.commit.html
When you have alot of transactions to make, say you are applying inserting items to the database from a loop, it will be better to use the mysqli_commit for this kind of process as it will only hit 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", "mark", "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", "mark", "joe", "alice", "bob"]

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

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


//Make a one-time hit 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 hit 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 queued ontill will explicitly call mysqli_commit($con)
2019-05-15 17:23:14
http://php5.kiev.ua/manual/ru/mysqli.commit.html

    Поддержать сайт на родительском проекте КГБ