Mid-Atlantic Developer Conference

mysqli::begin_transaction

mysqli_begin_transaction

(PHP 5 >= 5.5.0, PHP 7)

mysqli::begin_transaction -- mysqli_begin_transactionDémarre une transaction

Description

Style orienté objet (méthode):

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

Style procédural:

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

Commence une transaction. Nécessite le moteur InnoDB (il est actif par défaut). Pour plus de détails sur le fonctionnement des transactions MySQL, voir » http://dev.mysql.com/doc/mysql/en/commit.html.

Liste de paramètres

link

Seulement en style procédural : Un identifiant de lien retourné par la fonction mysqli_connect() ou par la fonction mysqli_init()

flags

Les drapeaux valides sont :

  • MYSQLI_TRANS_START_READ_ONLY : Commence la transaction comme "START TRANSACTION READ ONLY". Nécessite MySQL 5.6 ou supérieur.

  • MYSQLI_TRANS_START_READ_WRITE : Commence la transaction comme "START TRANSACTION READ WRITE". Nécessite MySQL 5.6 et supérieur.

  • MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT : Commence la transaction comme "START TRANSACTION WITH CONSISTENT SNAPSHOT".

name

Nom du point de sauvegarde pour la transaction.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.

Exemples

Exemple #1 Exemple avec $mysqli->begin_transaction()

Style orienté objet

<?php
$mysqli 
= new mysqli("127.0.0.1""my_user""my_password""sakila");

if (
$mysqli->connect_errno) {
    
printf("Echec de la connexion : %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();
?>

Style procédural

<?php
$link 
mysqli_connect("127.0.0.1""my_user""my_password""sakila");

if (
mysqli_connect_errno()) {
    
printf("Echec de la connexion : %s\n"mysqli_connect_error());
    exit();
}

mysqli_begin_transaction($linkMYSQLI_TRANS_START_READ_ONLY);

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

mysqli_close($link);
?>

Voir aussi

add a note add a note

User Contributed Notes 3 notes

up
4
Luc
2 years ago
For PHP<5.5:

    mysqli_query($db, "START TRANSACTION");
up
2
Ral
9 months ago
If you receive errors like: "This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required" with versions of MariaDB that DO support them, this is due to an internal check in mysqli conflicting with a hack in MariaDB to allow replication with oracle mysql.

MariaDB prefixes its server version numbers with "5.5.5-" for example "5.5.5-10.3.7-MariaDB-1:10.3.7+maria~stretch". This is because oracle mysql would interpet the "10" as version 1. Mysql clients aware of MariaDB have been updated to detect and strip this prefix.

However the check for mysqli.begin-transaction sees the 5.5.5 prefix and so fails.

The workaround is to specify a custom version string without the prefix for MariaDB on the command line using the --version option. Then mysqli.begin-transaction functions as expected.
up
0
VasK@hapir
3 months ago
The above answer from Ral worked for us, Thanks a lot. This is how we implemented the proposed workaround for

Warning: mysqli_begin_transaction(): This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required

We appended the following line to /etc/my.cnf and restarted MySQL server

version=10.2.19-MariaDB
To Top