mysqli::begin_transaction

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

mysqli::begin_transaction -- mysqli_begin_transaction — Starts a transaction


설명

객체 지향 스타일

public mysqli::begin_transaction(int $flags = 0, ?string $name = null): bool

절차적 스타일

mysqli_begin_transaction(mysqli $mysql, int $flags = 0, ?string $name = null): bool

거래를 시작합니다. InnoDB 엔진이 필요합니다(기본적으로 활성화됨). MySQL 트랜잭션 작동 방식에 대한 자세한 내용은 » http://dev.mysql.com/doc/mysql/en/commit.html을 참조하세요.


매개변수

mysql
절차적 스타일 전용: mysqli_connect() 또는 mysqli_init()에 의해 반환된 mysqli 객체
flags
유효한 플래그는 다음과 같습니다.
  • MYSQLI_TRANS_START_READ_ONLY: "START TRANSACTION 읽기 전용"으로 트랜잭션을 시작합니다. 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를 반환합니다.


변경 로그

버전 설명
8.0.0 name은 이제 nullable입니다.

Examples

예제 #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;
}
                  
                

노트

메모: 이 함수는 비 트랜잭션 테이블 유형(MyISAM 또는 ISAM과 같은)에서는 작동하지 않습니다.


기타