English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_commit() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_commit() function submits a transaction

Definition and Usage

MySQL database has a feature called automatic transaction commit (if you turn it on), then the changes made in the database will be automatically saved; if it is turned off, changes need to be explicitly saved.

mysqli_commit()The function saves the current transaction.

Syntax

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

Parameter

Serial NumberParameter and Description
1

con (required)

This is an object representing the connection with the MySQL Server.

2

flags (optional)

It can be one of the following constants:

  • 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, when given, will be executed with COMMIT /* name */to execute in the form of.

return value

The mysqli_commit() function returns a boolean value, which istrue,otherwisefalse.

PHP version

This function was originally introduced in PHP version5introduced and available in all higher versions.

Online Example

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 examples demonstratemysqli_commit()Function Usage (Procedural Style)-

<?php
   //Establish the connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Set the auto-commit to false
   mysqli_autocommit($con, False);
   //Insert the record 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')");
   //Verify the content of the table
   $result = mysqli_query($con, "SELECT * FROM my_team");
   print_r($result);
   mysqli_commit($con);
   //Close connection
   mysqli_close($con);
?>

Output the result

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

If you verify the content of the table my_team, you can observe the saved data as follows-

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

Online Example

Object-oriented style method, syntax:}}$con-> commit(). Here is an example of this function in object-oriented style;

//Establish the connection
$con = new mysqli("localhost", "root", "password", "mydb");
//Set the auto-commit to true
$con-> autocommit(FALSE);
//Insert the record 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')");
//Verify the content of the table
$result = $con-> query("SELECT * FROM my_team");
print_r($result);
//Save the result
$con-> commit();
//Close connection
$con -> close();
?>

Output the result

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

Online Example

Let's look at another example. Here, we create a table, disable the auto-commit option, insert a record, and save the changes. After saving, we insert another record:

//Establish the 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))");
//Set the auto-commit to false
mysqli_autocommit($con, False);
//Insert the record 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')");
//Close connection
mysqli_close($con);
?>

Since we have not saved the previous insert query yet, if you verify players If the content of the table is checked, then only one record can be seen as follows-

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

Online Example

<?php
   $connection = mysqli_connect("localhost", "root", "password", "mydb");
   
   if (mysqli_connect_errno($connection)){
      echo "Connection to MySQL failed: " . 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 the table is verifiedtestIf the content of the table is checked, then the inserted record can be seen as-

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

PHP MySQLi Reference Manual