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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP mysqli_begin_transaction() function

PHP MySQLi Reference Manual

The mysqli_begin_transaction() function starts a transaction

Definition and usage

mysqli_begin_transaction()Used to start a new transaction.

Syntax

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

Parameter

Serial numberParameters 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:

  • MYSQLI_TRANS_START_READ_ONLY

  • MYSQLI_TRANS_START_READ_WRITE

  • MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT

3

name (optional)

This is a string value representing the name of the transaction savepoint.

Return value

The PHP mysqli_begin_transaction() function returns a boolean value, and is true if the operation is successful.true,otherwisefalse.

PHP version

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

Online example

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...

Online example

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...

PHP MySQLi Reference Manual