For PHP<5.5:
mysqli_query($db, "START TRANSACTION");
(PHP 5 >= 5.5.0, PHP 7)
mysqli::begin_transaction -- mysqli_begin_transaction — Starts a transaction
Stile orientato agli oggetti
$flags
= 0
[, string $name
]] ) : boolStile procedurale:
Begins a transaction. Requires the InnoDB engine (it is enabled by default). For additional details about how MySQL transactions work, see » http://dev.mysql.com/doc/mysql/en/commit.html.
link
Solo nello stile procedurale: un identificatore restituito da mysqli_connect() o mysqli_init()
flags
Valid flags are:
MYSQLI_TRANS_START_READ_ONLY
:
Start the transaction as "START TRANSACTION READ ONLY".
Requires MySQL 5.6 and above.
MYSQLI_TRANS_START_READ_WRITE
:
Start the transaction as "START TRANSACTION READ WRITE".
Requires MySQL 5.6 and above.
MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT
:
Start the transaction as "START TRANSACTION WITH CONSISTENT SNAPSHOT".
name
Savepoint name for the transaction.
Restituisce true
in caso di successo, false
in caso di fallimento.
Nota:
This function does not work with non transactional table types (like MyISAM or ISAM).
Example #1 mysqli::begin_transaction() example
Stile orientato agli oggetti
<?php
/* Tell mysqli to throw an exception if an error occurs */
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* The table engine has to support transactions */
$mysqli->query("CREATE TABLE IF NOT EXISTS language (
Code text NOT NULL,
Speakers int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
/* Start transaction */
$mysqli->begin_transaction();
try {
/* Insert some values */
$mysqli->query("INSERT INTO language(Code, Speakers) VALUES ('DE', 42000123)");
/* Try to insert invalid values */
$language_code = 'FR';
$native_speakers = 'Unknown';
$stmt = $mysqli->prepare('INSERT INTO language(Code, Speakers) VALUES (?,?)');
$stmt->bind_param('ss', $language_code, $native_speakers);
$stmt->execute();
/* If code reaches this point without errors then commit the data in the database */
$mysqli->commit();
} catch (mysqli_sql_exception $exception) {
$mysqli->rollback();
throw $exception;
}
Stile procedurale
<?php
/* Tell mysqli to throw an exception if an error occurs */
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");
/* The table engine has to support transactions */
mysqli_query($mysqli, "CREATE TABLE IF NOT EXISTS language (
Code text NOT NULL,
Speakers int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
/* Start transaction */
mysqli_begin_transaction($mysqli);
try {
/* Insert some values */
mysqli_query($mysqli, "INSERT INTO language(Code, Speakers) VALUES ('DE', 42000123)");
/* Try to insert invalid values */
$language_code = 'FR';
$native_speakers = 'Unknown';
$stmt = mysqli_prepare($mysqli, 'INSERT INTO language(Code, Speakers) VALUES (?,?)');
mysqli_stmt_bind_param($stmt, 'ss', $language_code, $native_speakers);
mysqli_stmt_execute($stmt);
/* If code reaches this point without errors then commit the data in the database */
mysqli_commit($mysqli);
} catch (mysqli_sql_exception $exception) {
mysqli_rollback($mysqli);
throw $exception;
}
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.
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