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
Previous: affected_rows
Next: begin_transaction
PHP: Tips of the Day
To use the constant simply use its name:
Example:
if (EARTH_IS_FLAT) { print "Earth is flat"; } print APP_ENV_UPPERCASE;
Output:
or if you don't know the name of the constant in advance, use the constant function:
// this code is equivalent to the above code $const1 = "EARTH_IS_FLAT"; $const2 = "APP_ENV_UPPERCASE"; if (constant($const1)) { print "Earth is flat"; } print constant($const2);
Output:
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
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