如何在mysqli中开始和结束交易?
2022-08-30 11:26:25
据我所知,事务在调用语句后开始,在调用命令后结束,如下面的示例所示。$mysqli->autocommit(FALSE);
$mysqli->commit();
<?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
?>
我理解正确了吗?如果不能,请纠正我,因为这实际上是我第一次在现实生活中使用交易。
谢谢。