w3resource

PHP mysqli: autocommit() function

mysqli_autocommit() function / mysqli::autocommit

The mysqli_autocommit() function / mysqli::autocommit turns on or off the auto-commit mode on queries. auto-commit is a property which saves the changes made to database automatically if the mode is on.

Syntax:

Object oriented style

bool mysqli::autocommit ( bool $mode )

Procedural style

bool mysqli_autocommit ( mysqli $link , bool $mode )

Parameter:

Name Description Required/Optional
link A link identifier returned by mysqli_connect() or mysqli_init(). Required
mode Whether to turn on auto-commit or not. Required

Usage: Procedural style

mysqli_autocommit(connection,mode);

Parameter:

Name Description Required/Optional
connection Specifies the MySQL connection to use. Required
mode FALSE turns auto-commit off. TRUE turns auto-commit on (and commits any waiting queries) Required

Return value:

Returns TRUE on success or FALSE on failure.

Version: PHP 5, PHP 7

Example of object oriented style:

<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* turn autocommit on */
$mysqli->autocommit(TRUE);

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

/* close connection */
$mysqli->close();
?>

Output:

Autocommit is 1

Example of procedural style:

<?php
$link = mysqli_connect("localhost", "user1", "datasoft123", "hr");

if (!$link) {
    printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
    exit();
}

/* turn autocommit on */
mysqli_autocommit($link, TRUE);

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

/* close connection */
mysqli_close($link);
?>

Output:

Autocommit is 1

See also

PHP Function Reference

Previous: affected_rows
Next: begin_transaction



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php/function-reference/mysqli_autocommit.php