通过 PHP 启动时 SQL 事务不起作用


SQL transaction not working when initiated via PHP

我有以下查询,当它由我的PHP代码启动时不起作用:

$sql = 'START TRANSACTION;
        DELETE FROM task_actions
            WHERE task_id='.$id.';
        DELETE FROM tasks
            WHERE id='.$id.';
        COMMIT;
       ';

当我echo $sql并将输出直接放入phpMyAdmin时,它可以正常工作;当我分两步而不是一个事务完成它时,它也可以从我的PHP代码中工作。

我最初认为MySQL可能不允许事务,但 stackoverflow.com/questions/2050310 和 stackoverflow.com/questions/2960012 表明这是错误的。

我发现我可以禁用自动提交,执行查询并重新激活自动提交(stackoverflow.com/a/17607619 和 stackoverflow.com/a/12092151),但我不想这样做。

任何想法为什么它不起作用?

$sql = 'START TRANSACTION';
// run this query
$sql = 'DELETE FROM task_actions WHERE task_id=?';
// run this query
$sql = 'DELETE FROM tasks WHERE id=?';
// run this query
$sql = 'COMMIT';
// finally run this one