<?php $mysqli = new mysqli("localhost", "username", "password", "database"); // 检查连接是否成功 if ($mysqli->connect_error) { die("连接失败: " . $mysqli->connect_error); } // 开启一个事务 $mysqli->autocommit(false); // 执行一些SQL操作 $result1 = $mysqli->query("INSERT INTO table1 (column1) VALUES ('value1')"); $result2 = $mysqli->query("UPDATE table2 SET column2 = 'value2' WHERE id = 1"); // 检查操作是否都成功 if ($result1 && $result2) { // 提交事务 $mysqli->commit(); echo "事务成功"; } else { // 回滚事务 $mysqli->rollback(); echo "事务失败"; } // 重新开启自动提交 $mysqli->autocommit(true); // 关闭数据库连接 $mysqli->close(); ?>
标签:autocommit,事务,事务处理,connect,query,mysqli From: https://www.cnblogs.com/muqiaoxq/p/18114252