©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PHP 5)
mysqli::commit -- mysqli_commit — 提交一个事务
面向对象风格
过程化风格
$link
)提交数据库连接的当前事务
link
仅以过程化样式:由 mysqli_connect() 或 mysqli_init() 返回的链接标识。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 mysqli::commit() example
面向对象风格
<?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "world" );
if ( mysqli_connect_errno ()) {
printf ( "Connect failed: %s\n" , mysqli_connect_error ());
exit();
}
$mysqli -> query ( "CREATE TABLE Language LIKE CountryLanguage" );
$mysqli -> autocommit ( FALSE );
$mysqli -> query ( "INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)" );
$mysqli -> query ( "INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)" );
$mysqli -> commit ();
$mysqli -> query ( "DROP TABLE Language" );
$mysqli -> close ();
?>
过程化风格
<?php
$link = mysqli_connect ( "localhost" , "my_user" , "my_password" , "test" );
if (! $link ) {
printf ( "Connect failed: %s\n" , mysqli_connect_error ());
exit();
}
mysqli_autocommit ( $link , FALSE );
mysqli_query ( $link , "CREATE TABLE Language LIKE CountryLanguage" );
mysqli_query ( $link , "INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)" );
mysqli_query ( $link , "INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)" );
mysqli_commit ( $link );
mysqli_close ( $link );
?>
[#1] NoMan2000 [2015-05-27 14:51:00]
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.
[#2] snchzantonio at gmail dot com [2013-08-16 17:38:35]
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();
?>
[#3] Bob Johnson [2009-09-10 10:42:41]
The compactness of Lorenzo's code is admirable.
However, it is a good idea to also check $mysqli->affected_rows to make sure that the INSERT statement did not fail.
<?php
$result_query = @mysqli_query($query, $connect);
if (($result_query == false) &&
(mysqli_affected_rows($connect) == 0))
{
// verify the query executed completely and verify that it
// had impact on the table
$success = false;
// here also, the developer could choose to add a ROLLBACK
// statement
}
?>
[#4] mvanlamz [2009-03-31 11:36:59]
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.
[#5] Lorenzo - webmaster AT 4tour DOT it [2009-02-11 03:12:53]
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!