mysqli::autocommit

mysqli_autocommit

(PHP 5, PHP 7, PHP 8)

mysqli::autocommit -- mysqli_autocommitTurns on or off auto-committing database modifications

Açıklama

Nesne yönelimli kullanım

publicmysqli::autocommit(bool$enable): bool

Yordamsal kullanım

mysqli_autocommit(mysqli$mysql, bool$enable): bool

Turns on or off auto-commit mode on queries for the database connection.

To determine the current state of autocommit use the SQL command SELECT @@autocommit.

Bağımsız Değişkenler

bağlantı

Sadece yordamsal tarz: mysqli_connect() veya mysqli_init() işlevinden dönen bir mysqli nesnesi.

enable

Whether to turn on auto-commit or not.

Dönen Değerler

Başarı durumunda true, başarısızlık durumunda false döner.

Hatalar/İstisnalar

Eğer mysqli hata bildirimi etkinse (MYSQLI_REPORT_ERROR) ve istenen işlem başarısız olursa bir uyarı üretilir. Ek olarak, kip MYSQLI_REPORT_STRICT ise bunun yerine mysqli_sql_exception istisnası oluşur.

Örnekler

Örnek 1 mysqli::autocommit() example

Nesne yönelimli kullanım

<?php


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");


$mysqli->query("CREATE TABLE IF NOT EXISTS language (
Code text NOT NULL,
Speakers int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
);


$mysqli->autocommit(false);

$result = $mysqli->query("SELECT @@autocommit");
$row = $result->fetch_row();
printf("Autocommit is %s\n", $row[0]);

try {

$stmt = $mysqli->prepare('INSERT INTO language(Code, Speakers) VALUES (?,?)');
$stmt->bind_param('ss', $language_code, $native_speakers);


$language_code = 'DE';
$native_speakers = 50_123_456;
$stmt->execute();
$language_code = 'FR';
$native_speakers = 40_546_321;
$stmt->execute();


$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]);


$language_code = 'PL';
$native_speakers = 30_555_444;
$stmt->execute();
$language_code = 'DK';
$native_speakers = 5_222_444;
$stmt->execute();


$mysqli->autocommit(true);

print
"Committed 2 row in the database\n";
} catch (
mysqli_sql_exception $exception) {
$mysqli->rollback();

throw
$exception;
}

Yordamsal kullanım

<?php


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");


mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS language (
Code text NOT NULL,
Speakers int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
);


mysqli_autocommit($mysqli, false);

$result = mysqli_query($mysqli, "SELECT @@autocommit");
$row = mysqli_fetch_row($result);
printf("Autocommit is %s\n", $row[0]);

try {

$stmt = mysqli_prepare($mysqli, 'INSERT INTO language(Code, Speakers) VALUES (?,?)');
mysqli_stmt_bind_param($stmt, 'ss', $language_code, $native_speakers);


$language_code = 'DE';
$native_speakers = 50_123_456;
mysqli_stmt_execute($stmt);
$language_code = 'FR';
$native_speakers = 40_546_321;
mysqli_stmt_execute($stmt);


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]);


$language_code = 'PL';
$native_speakers = 30_555_444;
mysqli_stmt_execute($stmt);
$language_code = 'DK';
$native_speakers = 5_222_444;
mysqli_stmt_execute($stmt);


mysqli_autocommit($mysqli, true);

print
"Committed 2 row in the database\n";
} catch (
mysqli_sql_exception $exception) {
mysqli_rollback($mysqli);

throw
$exception;
}

Yukarıdaki örneklerin çıktısı:

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

Notlar

Bilginize:

This function does not work with non transactional table types (like MyISAM or ISAM).

Ayrıca Bakınız

To Top