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_begin_transaction() Function.
The built-in PHP mysqli_begin_transaction() function is used to start a new transaction.
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.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 :
|
3 | name(Optional) This is string value representing the name of the save point of the transaction. |
Return Value
This functions returns TRUE if the operation is successful and, FALSE if not.
PHP Version
This built-in function was first introduced in PHP version 5 and it works in all of the later versions.
Example1
Following example demonstrates the usage of the 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 are going to be studying about the PHP mysqli_change_user() 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.
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.