mysqli::autocommit

mysqli_autocommit

(PHP 5, PHP 7, PHP 8)

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

Description

Object-oriented style

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

Procedural style

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.

Parameters

mysql

Procedural style only: A mysqli object returned by mysqli_connect() or mysqli_init()

enable

Whether to turn on auto-commit or not.

Return Values

Returns true on success or false on failure.

Examples

Example #1 mysqli::autocommit() example

Object-oriented style

<?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;
}

Procedural style

<?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($mysqlifalse);

$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($mysqlitrue);

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

    throw 
$exception;
}

The above examples will output:

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

Notes

Note:

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

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial