re1man
re1man

Reputation: 2367

multiple queries in a transaction and committing/rolling back with php

Lets say I have these three mysql queries:

SELECT customer_id FROM customers WHERE name = 'John'
INSERT INTO quests_on (quest_id, name, quest_points) VALUES ('1', 'John', '15)
UPDATE badges_on SET badge_owned = 'owned' WHERE quest_id = '1'

How would I use a mysql transaction to ensure that all changes are made else rollback the changes. Would using the PDO extension in php suffice here? Just needed some clarity. I was also wondering if you could have SELECT queries in a transaction and that if transactions can take different paths (such as if the value from the SELECT query exists or not)

Upvotes: 1

Views: 1981

Answers (1)

Nameless
Nameless

Reputation: 2366

$db = new PDO($dsn, $user, $password);
$db->beginTransaction();
try {
  // do your queries here, using PDO and $db object
  $db->commit();
} catch (PDOException $e) {
  $db->rollback();
}

For more information, please read http://www.php.net/manual/en/book.pdo.php

Upvotes: 2

Related Questions