如何在mysqli中开始和结束事务? [英] How to start and end transaction in mysqli?

查看:89
本文介绍了如何在mysqli中开始和结束事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,事务如下所示,一旦调用$mysqli->autocommit(FALSE);语句即开始,并在调用$mysqli->commit();命令后终止.

As far as I understood transaction starts once we call $mysqli->autocommit(FALSE); statement and ends after calling $mysqli->commit(); command like in the example below.

<?php
//Start transaction 
$mysqli->autocommit(FALSE);
$mysqli->query('UPDATE `table` SET `col`=2');
$mysqli->query('UPDATE `table1` SET `col1`=3;');
$mysqli->commit();
//End transaction

//Executing other queries without transaction control
$mysqli->query("Select * from table1");
$mysqli->query("Update table1 set col1=2");
//End of executing other queries without transaction control

//Start transaction 
$mysqli->autocommit(FALSE);
$mysqli->query('UPDATE `table` SET `col`=2');
$mysqli->query('UPDATE `table1` SET `col1`=3;');
$mysqli->commit();
//End transaction
?>

我理解正确吗?如果不能,请指正我,因为这实际上是我第一次在现实生活中使用交易.

Have I understood correctly? If not could you please correct me, because it is actually my first time using transactions in real life.

谢谢.

推荐答案

根据

在上面的示例中:

  • CREATE TABLE是自动提交的,因为它是默认行为.
  • 由于autocommit(FALSE)
  • INSERT INTO 没有自动提交.
  • DROP TABLE是自动提交的,因为autocommit(FALSE)已由->commit(); 重置.
  • the CREATE TABLE is auto committed because it's the default behaviour.
  • the INSERT INTO aren't auto committed because of the autocommit(FALSE).
  • the DROP TABLE is auto committed because the autocommit(FALSE) was reset by the ->commit();.

这篇关于如何在mysqli中开始和结束事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆