downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysqli->connect_errno> <mysqli::close
Last updated: Fri, 13 Nov 2009

view this page in

mysqli::commit

mysqli_commit

(PHP 5)

mysqli::commit -- mysqli_commit現在のトランザクションをコミットする

説明

オブジェクト指向型(メソッド)

bool mysqli::commit ( void )

手続き型形式:

bool mysqli_commit ( mysqli $link )

データベース接続の現在のトランザクションをコミットします。

パラメータ

link

手続き型のみ: mysqli_connect() あるいは mysqli_init() が返すリンク ID。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例1 オブジェクト指向形式

<?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 Type=InnoDB");

/* autocommit を off にします */
$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();
?>

例2 手続き型形式

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

/* 接続をチェックします */
if (!$link) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

/* autocommit を off にします */
mysqli_autocommit($linkFALSE);

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

/* データを挿入します */
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);
?>

参考

  • mysqli_autocommit() - データベース更新の自動コミットをオンまたはオフにする
  • mysqli_rollback() - 現在のトランザクションをロールバックする



mysqli->connect_errno> <mysqli::close
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
mysqli::commit
Bob Johnson
10-Sep-2009 05:42
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
               
}
?>
mvanlamz
31-Mar-2009 07:36
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.
Lorenzo - webmaster AT 4tour DOT it
11-Feb-2009 11:12
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!

mysqli->connect_errno> <mysqli::close
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites