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



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

PHP: How to generate a random, unique, alphanumeric string for use in a secret link?

Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand() and uniqid() are not cryptographically secure random number generators. See Scott's answer for a secure alternative.

If you do not need it to be absolutely unique over time:

md5(uniqid(rand(), true))

Otherwise (given you have already determined a unique login for your user):

md5(uniqid($your_user_login, true))

Ref : https://bit.ly/31fd9wa

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook