English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_begin_transaction() function starts a transaction
mysqli_begin_transaction()Used to start a new transaction.
mysqli_begin_transaction($con, [$flags, $name]);
Serial number | Parameters and descriptions |
---|---|
1 | con (required) This is an object representing the connection to the MySQL Server. |
2 | flags (optional) A constant that can be one of the following values:
|
3 | name (optional) This is a string value representing the name of the transaction savepoint. |
The PHP mysqli_begin_transaction() function returns a boolean value, and is true if the operation is successful.true,otherwisefalse.
This function was originally introduced in PHP version5introduced and available in all higher versions.
The following example demonstratesmysqli_begin_transaction()Function usage (procedural program style)-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Start transaction mysqli_begin_transaction($con, MYSQLI_TRANS_START_READ_ONLY); print("Transaction started...\n"); //Create table mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); print("Table created...\n"); //Insert value mysqli_query($con, "INSERT INTO Test values('Raju', 25),('Rahman', 30),('Sarmista', 27); print("Insert record...\n"); //Commit transaction mysqli_commit($con); print("Transaction save...\n"); //Close connection mysqli_close($con); ?>
Output result
Transaction started... Table created... Insert record... Transaction save...
The syntax of the object-oriented style method is $con->begin_transaction(); Here is an example of this function in an object-oriented style;
//Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); //Start transaction $con->begin_transaction($con, MYSQLI_TRANS_START_READ_ONLY); print("Transaction started...\n"); //Create table $con->query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); print("Table created...\n"); //Insert value $con->query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27); print("Insert record...\n"); //Commit transaction $con->commit(); print("Transaction save...\n"); //Close connection $con->close(); ?>
Output result
Transaction started... Table created... Insert record... Transaction save...