PHP | mysqli_autocommit() Function

PHP mysqli_autocommit() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_autocommit() Function.

MYSQL database have a feature known as autocommit. If turned on, the change done in the databases are saved automatically and, if turned off, then you need to save all the changes explicitly. This function turns on/off the auto-commit feature.

This function accepts a boolean value as a parameter. If you pass true to this function, then the autocommit feature is going to be turned on and if you pass false, it turns off the auto-commit feature.

Syntax

Following below is the syntax to use this function -

mysqli_autocommit($con, $mode);


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

2

mode(Mandatory)

This is an boolean value representing whether the auto-commit mode should be turned on or not.


Return Value

This built-in PHP function returns TRUE on success and FALSE on failure.

PHP Version

This built-in function was first introduced in PHP version 5 and it works in all of the later versions.

Example1

Assuming we have created a table named my_team in the database mydb, as follows -

CREATE TABLE my_team(
   ID INT PRIMARY KEY AUTO_INCREMENT,
   First_Name VARCHAR(255), 
   Last_Name VARCHAR(255), 
   Place_Of_Birth VARCHAR(255), 
   Country VARCHAR(255)
);

The following example demonstrates the usage of the PHP mysqli_autocommit() function (in procedural style) -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Setting auto commit to false
   mysqli_autocommit($con, False);

   //Inserting a records into the my_team table
   mysqli_query($con, "insert into my_team values(1, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')");
   mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "insert into my_team values(3, 'Queen', 'Dauglas', 'Texas', 'UnitedStates')");
   mysqli_query($con, "insert into my_team values(4, 'Paul', 'Francis', 'Texas', 'UnitedStates')");

   //Verifying the contents of the table
   $result = mysqli_query($con, "SELECT * FROM my_team");
   print_r($result);

   //Closing the connection
   mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result -

mysqli_result Object
(
    [current_field] => 0
    [field_count] => 5
    [lengths] =>
    [num_rows] => 4
    [type] => 0
)

Since we have turned off the auto-commit option in the previous example, the records added won't be saved in the database and, if you verify the table contents in MYSQL, it will be empty as shown below $minus;

mysql> select * from my_team;
Empty set (0.00 sec)

To save the changes in the database you need to commit the changes at the end of the program using the mysqli_commit() function as -

mysqli_commit($con);

If you verify the table contents my_team, then you will see the inserted records as shown below -

mysql> select * from my_team;
+----+------------+------------+----------------+-------------+
| ID | First_Name | Last_Name  | Place_Of_Birth | Country     |
+----+------------+------------+----------------+-------------+
|  1 | Kennedy    | Nkpara     | PortHarcourt   | Nigeria     |
|  2 | Jonathan   | Trott      | CapeTown       | SouthAfrica |
|  3 | Queen      | Douglas    | Texas          | UnitedStates|
|  4 | Paul       | Francis    | Texas          | UnitedStates|
+----+------------+------------+----------------+-------------+
4 rows in set (0.00 sec)

Example2

In an object oriented style the syntax of this function is $con->autocommit. Following is an example of the php mysqli_autocommit() function in object oriented mode $minus;

//Creating a connection
$con = new mysqli("localhost", "root", "password", "mydb");

//Setting auto commit to true
$con->autocommit(FALSE);

//Inserting a records into the my_team table
$con->query( "insert into my_team values(1, 'Kennedy', 'Nkara', 'PortHarcourt', 'Nigeria')");
$con->query( "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
$con->query( "insert into my_team values(3, 'Queen', 'Douglas', 'Texas', 'UnitedStates')");
$con->query( "insert into my_team values(4, 'Paul', 'Francis', 'Texas', 'UnitedStates')");

//Verifying the contents of the table
$result = $con->query( "SELECT * FROM my_team");
print_r($result);

//Saving the results
$con->commit();

//Closing the connection
$con -> close();
?>

Output

When the above code is executed, it will produce the following result -

mysqli_result Object
(
    [current_field] => 0
    [field_count] => 5
    [lengths] =>
    [num_rows] => 4
    [type] => 0
)

Example3

The mysqli_autocommit() function also works as commit(). On invoking it saves the results of waiting queries to the database -

//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "mydb");

//creating a table
mysqli_query($con, "Create table players (First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))");

//Setting auto commit to false
mysqli_autocommit($con, False);

//Inserting a records into the my_team table
mysqli_query($con, "insert into players values('Kennedy', 'Nkpara', 'Nigeria')");
mysqli_query($con, "insert into players values('Jonathan', 'Trott', 'SouthAfrica')");

mysqli_autocommit($con, TRUE);

//Closing the connection
mysqli_close($con);
?>

Output

When the above code is executed, if you verify the contents of the players table, you can observe the added records, as shown below -

mysql> select * from players;
+------------+-----------+-------------+
| First_Name | Last_Name | Country     |
+------------+-----------+-------------+
| Kennedy    | Nkpara    | Nigeria     |
| Jonathan   | Trott     | SouthAfrica |
+------------+-----------+-------------+
2 rows in set (0.00 sec)

Example4

You can try the following example below -

<?php
   $connection = mysqli_connect("localhost", "root", "password", "mydb");
   
   if (mysqli_connect_errno($connection)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   
   mysqli_autocommit($connection,FALSE);   
   
   mysqli_query($connection, "create table test(Name VARCHAR(255), Age INT)");   
   
   mysqli_query($connection, "INSERT INTO test VALUES ('Bethel', 27)");
   mysqli_query($connection, "INSERT INTO test VALUES ('Blessing', 30)");
   
   mysqli_commit($connection);
   mysqli_close($connection);
?>

Output

When the above code is executed, if you verify the contents of the table test, you can see the inserted records as shown below -

mysql> select * from test;
+---------+------+
| Name    | Age  |
+---------+------+
| Bethel  |   27 |
| Blessing|   30 |
+---------+------+
2 rows in set (0.00 sec)


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP mysqli_begin_transaction() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.