Pascal
Pascal

Reputation: 443

SQL transaction not working when initiated via PHP

I have the following query which does not work when it is initiated by my PHP code:

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

When I echo $sql and put the output directly into phpMyAdmin, it works without a problem; and when I had it done in two steps instead of one transaction, it worked from my PHP code, too.

I first thought MySQL might not allow transactions, but stackoverflow.com/questions/2050310 and stackoverflow.com/questions/2960012 showed that was wrong.

I found I could disable autocommit, do both queries and reactivate autocommit (stackoverflow.com/a/17607619 & stackoverflow.com/a/12092151), but I would prefer not to.

Any ideas why it does not work?

Upvotes: 0

Views: 129

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 158007

$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

Upvotes: 1

Related Questions