0

Trying to make a transaction, so only if all the queries will succeed, they are executed. I did a lot of them in procedural, but first time in OOP style. I've seen PHP + MySQL transactions examples, and How to start and end transaction in mysqli?, no result.

So I have this table:

CREATE TABLE `foo` (
  `id` int(11) NOT NULL,
  `bar` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

And this two queries:

$connection->autocommit(FALSE);
$connection->query("INSERT INTO foo (bar) VALUES ('1')");
$connection->query("INSERT INTO foo (bar) VALUES ('a')");
if($connection->commit()){
    echo "Commit";
} else {
    echo "Rollback";
}

As bar is INT, the first query will succeed, and the second fail. But although I am using a transaction and the second query fails, the first one is executed, and I don't understand why.

What I am doing wrong?

0 Answers0