PHP | mysqli_begin_transaction() Function

PHP mysqli_begin_transaction() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP mysqli_begin_transaction() Function.

The built-in PHP mysqli_begin_transaction() function is used to start a new transaction.

Syntax

Following below is the syntax to use this function -

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


Parameter Details

Sr.NoParameter & 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_START_READ_ONLY

  • MYSQLI_TRANS_START_READ_WRITE

  • MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT

3

name(Optional)

This is string value representing the name of the save point of the transaction.


Return Value

This built-in PHP functions returns true if the operation is successful and, false if not.

PHP Version

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

Example1

Following example demonstrates the usage of the PHP mysqli_begin_transaction() function (in procedural style) -

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

   //Beginning the transaction
   mysqli_begin_transaction($con, MYSQLI_TRANS_START_READ_ONLY);
   print("Transaction Started......\n");

   //Creating a table
   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Table Created......\n");

   //Inserting values
   mysqli_query($con, "INSERT INTO Test values('Paul', 25),('Peter', 30),('Kennedy', 27)");
   print("Records Inserted......\n");

   //Committing the transaction
   mysqli_commit($con);
   print("Transaction Saved......\n");

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

Output

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

Transaction Started......
Table Created......
Records Inserted......
Transaction Saved......

Example2

In object oriented style, the syntax for this PHP function is $con->begin_transaction(). Following is an example of this function in an object oriented style $minus;

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

//Beginning the transaction
$con->begin_transaction($con, MYSQLI_TRANS_START_READ_ONLY);
print("Transaction Started......\n");

//Creating a table
$con->query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
print("Table Created......\n");

//Inserting values
$con->query("insert into Test values('Paul', 25),('Peter', 30),('Kennedy', 27)");
print("Records Inserted......\n");

//Committing the transaction
$con->commit();
print("Transaction Saved......\n");

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

Output

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

Transaction Started......
Table Created......
Records Inserted......
Transaction Saved......


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will discuss about the mysqli_change_user() Function in PHP.

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.