Php/docs/mysqli.begin-transaction

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

mysqli::begin_transaction

mysqli_begin_transaction

(PHP 5 >= 5.5.0, PHP 7)

mysqli::begin_transaction -- mysqli_begin_transactionトランザクションを開始する


説明

オブジェクト指向型

public mysqli::begin_transaction ([ int $flags = 0 [, string $name ]] ) : bool

手続き型:

mysqli_begin_transaction ( mysqli $link [, int $flags = 0 [, string $name ]] ) : bool

トランザクションを開始します。InnoDB エンジン (デフォルトで有効になっています) が必要です。 MySQL のトランザクションの詳細な動作は、 » http://dev.mysql.com/doc/mysql/en/commit.html を参照ください。


パラメータ

link
手続き型のみ: mysqli_connect() あるいは mysqli_init() が返すリンク ID。
flags
以下のフラグが使えます。
MYSQLI_TRANS_START_READ_ONLY
  • "START TRANSACTION READ ONLY" でトランザクションを開始します。 MySQL 5.6 以上が必要です。
  • MYSQLI_TRANS_START_READ_WRITE: "START TRANSACTION READ WRITE" でトランザクションを開始します。 MySQL 5.6 以上が必要です。
  • MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT: "START TRANSACTION WITH CONSISTENT SNAPSHOT" でトランザクションを開始します。
name
トランザクションのセーブポイント名。


返り値

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


注意

注意:

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

例1 mysqli::begin_transaction() の例

オブジェクト指向型


<?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;");/* Start transaction */$mysqli->begin_transaction();try {    /* Insert some values */    $mysqli->query("INSERT INTO language(Code, Speakers) VALUES ('DE', 42000123)");    /* Try to insert invalid values */    $language_code = 'FR';    $native_speakers = 'Unknown';    $stmt = $mysqli->prepare('INSERT INTO language(Code, Speakers) VALUES (?,?)');    $stmt->bind_param('ss', $language_code, $native_speakers);    $stmt->execute();    /* If code reaches this point without errors then commit the data in the database */    $mysqli->commit();} 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;");/* Start transaction */mysqli_begin_transaction($mysqli);try {    /* Insert some values */    mysqli_query($mysqli, "INSERT INTO language(Code, Speakers) VALUES ('DE', 42000123)");    /* Try to insert invalid values */    $language_code = 'FR';    $native_speakers = 'Unknown';    $stmt = mysqli_prepare($mysqli, 'INSERT INTO language(Code, Speakers) VALUES (?,?)');    mysqli_stmt_bind_param($stmt, 'ss', $language_code, $native_speakers);    mysqli_stmt_execute($stmt);    /* If code reaches this point without errors then commit the data in the database */    mysqli_commit($mysqli);} catch (mysqli_sql_exception $exception) {    mysqli_rollback($mysqli);    throw $exception;}?>

参考