w3resource

PHP mysqli: begin_transaction() function

mysqli_begin_transaction function / mysqli::begin_transaction

The mysqli_begin_transaction function / mysqli::begin_transaction — Starts a transaction

Syntax:

Object oriented style(method)

public bool mysqli::begin_transaction ([ int $flags [, string $name ]] )

Procedural style

bool mysqli_begin_transaction ( mysqli $link [, int $flags [, string $name ]] )

Usage: Procedural style

begin_transaction ([ int $flags [, string $name ]] );

Parameter:

Name Description Required/Optional
Link Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init() Required for Procedural style and Optional for object oriented style
flags Valid flags are:
MYSQLI_TRANS_START_READ_ONLY: Start the transaction as "START TRANSACTION READ ONLY".
MYSQLI_TRANS_START_READ_WRITE: Start the transaction as "START TRANSACTION READ WRITE".
MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT: Start the transaction as "START TRANSACTION WITH
Riquired
name Savepoint name for the transaction.

Return value:

Returns TRUE on success or FALSE on failure.

Version: PHP 5, PHP 7

Example of object oriented style:

<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$mysqli->begin_transaction(MYSQLI_TRANS_START_READ_ONLY);

$mysqli->query("SELECT first_name, last_name FROM actor");
$mysqli->commit();

$mysqli->close();
?>

Example of procedural style:

<?php
$link = mysqli_connect("localhost", "user1", "datasoft123", "hr");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

mysqli_begin_transaction($link, MYSQLI_TRANS_START_READ_ONLY);

mysqli_query($link, "SELECT first_name, last_name FROM actor LIMIT 1");
mysqli_commit($link);

mysqli_close($link);
?>

See also

PHP Function Reference

Previous: autocommit
Next: change_user



Follow us on Facebook and Twitter for latest update.