例如
$conn = new PDO("mysql:host=127.0.0.1;dbname=test", 'root', '123456');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
try {
$sql = "CREATE TABLE IF NOT EXISTS test2 (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`text` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
$conn->exec($sql);
$conn->commit();
} catch (\Exception $e) {
$conn->rollBack();
}
它在 PHP 7.4 中运行良好,但在 PHP 8.0 中无法运行,并且在线抛出异常$conn->commit();
:
PDOException:没有活动事务