Php/docs/mysqli.autocommit

提供:Dev Guides
< Php
移動先:案内検索

mysqli::autocommit

mysqli_autocommit

(PHP 5, PHP 7)

mysqli::autocommit -- mysqli_autocommitデータベース更新の自動コミットをオンまたはオフにする


説明

オブジェクト指向型

public mysqli::autocommit ( bool $mode ) : bool

手続き型

mysqli_autocommit ( mysqli $link , bool $mode ) : bool

指定されたデータベース接続リソースに対するクエリの 自動コミットモードをオンまたはオフにします。

現在の自動コミットモードを知るには、SQL コマンド SELECT @@autocommit を使用します。


パラメータ

link
手続き型のみ: mysqli_connect() あるいは mysqli_init() が返すリンク ID。
mode
自動コミットを有効にするかどうか。


返り値

成功した場合に true を、失敗した場合に false を返します。


注意

注意:

この関数は、トランザクションに 対応していないテーブル型(MyISAM あるいは ISAM など)では 動作しません。

例1 mysqli::autocommit() の例

オブジェクト指向型


<?php/* Tell mysqli to throw an exception if an error occurs */mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* The table engine has to support transactions */$mysqli->query("CREATE TABLE IF NOT EXISTS language (    Code text NOT NULL,    Speakers int(11) NOT NULL    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");/* Turn autocommit off */$mysqli->autocommit(false);$result = $mysqli->query("SELECT @@autocommit");$row = $result->fetch_row();printf("Autocommit is %s\n", $row[0]);try {    /* Prepare insert statement */    $stmt = $mysqli->prepare('INSERT INTO language(Code, Speakers) VALUES (?,?)');    $stmt->bind_param('ss', $language_code, $native_speakers);    /* Insert some values */    $language_code = 'DE';    $native_speakers = 50_123_456;    $stmt->execute();    $language_code = 'FR';    $native_speakers = 40_546_321;    $stmt->execute();    /* Commit the data in the database. This doesn't set autocommit=true */    $mysqli->commit();    print "Committed 2 rows in the database\n";    $result = $mysqli->query("SELECT @@autocommit");    $row = $result->fetch_row();    printf("Autocommit is %s\n", $row[0]);    /* Try to insert more values */    $language_code = 'PL';    $native_speakers = 30_555_444;    $stmt->execute();    $language_code = 'DK';    $native_speakers = 5_222_444;    $stmt->execute();    /* Setting autocommit=true will trigger a commit */    $mysqli->autocommit(true);    print "Committed 2 row in the database\n";} catch (mysqli_sql_exception $exception) {    $mysqli->rollback();    throw $exception;}

手続き型


<?php/* Tell mysqli to throw an exception if an error occurs */mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");/* The table engine has to support transactions */mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS language (    Code text NOT NULL,    Speakers int(11) NOT NULL    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");/* Turn autocommit off */mysqli_autocommit($mysqli, false);$result = mysqli_query($mysqli, "SELECT @@autocommit");$row = mysqli_fetch_row($result);printf("Autocommit is %s\n", $row[0]);try {    /* Prepare insert statement */    $stmt = mysqli_prepare($mysqli, 'INSERT INTO language(Code, Speakers) VALUES (?,?)');    mysqli_stmt_bind_param($stmt, 'ss', $language_code, $native_speakers);    /* Insert some values */    $language_code = 'DE';    $native_speakers = 50_123_456;    mysqli_stmt_execute($stmt);    $language_code = 'FR';    $native_speakers = 40_546_321;    mysqli_stmt_execute($stmt);    /* Commit the data in the database. This doesn't set autocommit=true */    mysqli_commit($mysqli);    print "Committed 2 rows in the database\n";    $result = mysqli_query($mysqli, "SELECT @@autocommit");    $row = mysqli_fetch_row($result);    printf("Autocommit is %s\n", $row[0]);    /* Try to insert more values */    $language_code = 'PL';    $native_speakers = 30_555_444;    mysqli_stmt_execute($stmt);    $language_code = 'DK';    $native_speakers = 5_222_444;    mysqli_stmt_execute($stmt);    /* Setting autocommit=true will trigger a commit */    mysqli_autocommit($mysqli, true);    print "Committed 2 row in the database\n";} catch (mysqli_sql_exception $exception) {    mysqli_rollback($mysqli);    throw $exception;}

上の例の出力は以下となります。


Autocommit is 0
Committed 2 rows in the database
Autocommit is 0
Committed 2 row in the database
Autocommit is 0
Committed 2 rows in the database
Autocommit is 0
Committed 2 row in the database

参考