php-mysqlHow to begin MySQL transaction in PHP?
MySQL transactions can be started in PHP using the mysqli_begin_transaction()
function. This function takes a single parameter, which is a mysqli
object.
$mysqli = new mysqli("localhost", "user", "password", "database");
if (mysqli_begin_transaction($mysqli)) {
// Transaction started successfully
} else {
// Transaction failed to start
}
The code above creates a new mysqli
object and then attempts to start a transaction using the mysqli_begin_transaction()
function. If the transaction is started successfully, the code inside the if
block will be executed. Otherwise, the code inside the else
block will be executed.
Code explanation
mysqli_begin_transaction()
: This function is used to start a MySQL transaction.$mysqli
: This is amysqli
object that is used to connect to the MySQL database.if
: This is a control structure that is used to execute code based on a condition.else
: This is a control structure that is used to execute code if the condition in theif
statement is not met.
Helpful links
More of Php Mysql
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to join tables with PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to count the number of resulting rows in a MySQL database using PHP?
- How to use a MySQL union in PHP?
- How to set a timeout for MySQL query in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to get the last insert ID in PHP MySQL?
- How to fetch data from MySQL in PHP?
See more codes...