• PHP Video Tutorials

PHP mysqli_commit() Function



Definition and Usage

MySQL database have a feature named auto-commit if you turn it on, the changes done in the databases are saved automatically and, If you turn it off, you need to save the changes explicitly.

The mysqli_commit() function saves the current transaction.

Syntax

mysqli_commit($con, [$flags, $name]);

Parameters

Sr.No Parameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

2

flags(Optional)

A constant which can be on of the following :

  • MYSQLI_TRANS_COR_AND_CHAIN

  • MYSQLI_TRANS_COR_AND_NO_CHAIN

  • MYSQLI_TRANS_COR_RELEASE

  • MYSQLI_TRANS_COR_NO_RELEASE

3

name(Optional)

This is a name value which when given, executes as COMMIT/*name*/ .

Return Values

The PHP mysqli_commit() function returns a boolean value which is, true if the commit operation is successful and, false if not.

PHP Version

This function was first introduced in PHP Version 5 and works in all the later versions.

Example

Assume 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)
);

Following example demonstrates the usage of the mysqli_commit() 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, 'Shikhar', 'Dhawan', 'Delhi', 'India')");
   mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "insert into my_team values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   mysqli_query($con, "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')");

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

   mysqli_commit($con);

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

This will produce following result −

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

If you and verify the contents of the table my_team then, you can observe the saved data as shown below −

mysql> select * from my_team;
+----+------------+------------+----------------+-------------+
| ID | First_Name | Last_Name  | Place_Of_Birth | Country     |
+----+------------+------------+----------------+-------------+
|  1 | Shikhar    | Dhawan     | Delhi          | India       |
|  2 | Jonathan   | Trott      | CapeTown       | SouthAfrica |
|  3 | Kumara     | Sangakkara | Matale         | Srilanka    |
|  4 | Virat      | Kohli      | Delhi          | India       |
+----+------------+------------+----------------+-------------+
4 rows in set (0.00 sec)

Example

The syntax of this method in object oriented style is $con->commit(). Following is an example of this 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, 'Shikhar', 'Dhawan', 'Delhi', 'India')");
$con->query( "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
$con->query( "insert into my_team values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
$con->query( "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')");

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

This will produce following result −

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

Example

Let us consider another example. In here, we have created a table, turned the auto-commit option off and inserted a record and saved the changes. After the save we have inserted another record −

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

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('Shikhar', 'Dhawan', 'India')");

mysqli_commit($con);

mysqli_query($con, "insert into players values('Jonathan', 'Trott', 'SouthAfrica')");

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

Since we havent saved the last insert query, after executing the above program if you verify the contents of the players table you can see only one record, as shown below −

mysql> select * from players;
+------------+-----------+---------+
| First_Name | Last_Name | Country |
+------------+-----------+---------+
| Shikhar    | Dhawan    | India   |
+------------+-----------+---------+
1 row in set (0.00 sec)

Example

<?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 ('Sharukh', 25)");
   mysqli_query($connection, "INSERT INTO test VALUES ('Kalyan', 30)");
   
   mysqli_commit($connection);
   mysqli_close($connection);
?>

After executing the above program if you verify, the contents of the table test, you can see the inserted records as −

mysql> select * from test;
+---------+------+
| Name    | Age  |
+---------+------+
| Sharukh |   25 |
| Kalyan  |   30 |
+---------+------+
2 rows in set (0.00 sec)
php_function_reference.htm
Advertisements