// create a PDO object and begin a transaction $dbh = new PDO('mysql:host=localhost;dbname=myDB', $user, $pass); $dbh->beginTransaction(); // perform database operations $stmt = $dbh->prepare("INSERT INTO users (username, password) VALUES (?, ?)"); $stmt->execute(array("john", "password123")); // commit the transaction $dbh->commit();
// create a PDO object and begin a transaction $dbh = new PDO('mysql:host=localhost;dbname=myDB', $user, $pass); $dbh->beginTransaction(); // perform database operations $stmt1 = $dbh->prepare("INSERT INTO users (username, password) VALUES (?, ?)"); $stmt1->execute(array("john", "password123")); $stmt2 = $dbh->prepare("INSERT INTO orders (user_id, item_name) VALUES (?, ?)"); $stmt2->execute(array($dbh->lastInsertId(), "Product A")); // commit the transaction $dbh->commit();In this example, we are creating a PDO object and beginning a transaction. We are then inserting a new user into the database and retrieving the last inserted ID. We are then inserting a new order for that user and committing the transaction. The PDO commit method is part of the PDO package library.