$dbo->beginTransaction(); // Start transaction
$dbo->commit(); // Commit the transaction
$dbo->rollBack(); // Rollback in case of error
4. Handling Exceptions in Transactions
try {
$dbo->beginTransaction();
$stmt = $dbo->prepare("UPDATE student SET mark = mark + 10 WHERE id = ?");
$stmt->execute([1]);
$dbo->commit();
echo " Number of record updated ".$stmt->rowCount();
} catch (PDOException $e) {
$dbo->rollBack();
echo "Failed: " . $e->getMessage();
}
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbo->exec("SAVEPOINT savepoint1");
$dbo->rollBack("savepoint1"); // Roll back to savepoint
Adding two records to student table, only one will be stored.
try {
$dbo->beginTransaction(); // Start outer transaction
// Insert first record into the student table
$dbo->exec("INSERT INTO student (name, class, mark, gender) VALUES ('John Doe', 'Six', 85, 'Male')");
$id1 = $dbo->lastInsertId();
echo "Inserted John Doe with ID: $id1 <br>";
// Create a savepoint
$dbo->exec("SAVEPOINT savepoint1");
// Insert second record into the student table
$dbo->exec("INSERT INTO student (name, class, mark, gender) VALUES ('Ronnee', 'Seven', 90, 'Female')");
$id2 = $dbo->lastInsertId();
echo "Inserted Ronee with ID: $id2 <br>";
// Rollback to savepoint1 (undoes Jane Doe's insert)
$dbo->exec("ROLLBACK TO savepoint1");
$dbo->commit(); // Commit the transaction (only 'John Doe' will be saved)
} catch (Exception $e) {
$dbo->rollBack(); // Roll back the entire transaction
echo "Transaction failed: " . $e->getMessage();
}
The output ( id 43 and 44 ) cames for two records but in the database table only 43 will be available.
Inserted John Doe with ID: 43
Inserted Ronee with ID: 44
$dbo->beginTransaction();
$stmt = $dbo->prepare("INSERT INTO student (name, class, mark, gender) VALUES (?, ?, ?, ?)");
$stmt->execute(['John', '10th', 85, 'Male']);
$dbo->commit();
Here's an example of using transactions with try-catch for updating a student's data:
try {
// Begin the transaction
$dbo->beginTransaction();
// Update student mark
$stmt = $dbo->prepare("UPDATE student SET mark = :mark WHERE id = :id");
$stmt->bindParam(':mark', $newMark);
$stmt->bindParam(':id', $studentId);
$newMark = 90;
$studentId = 1;
$stmt->execute();
// Commit the transaction
$dbo->commit();
echo "Transaction successful!";
} catch (PDOException $e) {
// Rollback if there is an error
$dbo->rollBack();
echo "Transaction failed: " . $e->getMessage();
}
Transaction Begin: beginTransaction() starts the transaction.try {
$dbo->beginTransaction();
$stmt = $dbo->prepare("INSERT INTO student (name, class, mark, gender) VALUES (?, ?, ?, ?)");
$stmt->execute(['Alice', '10th', 88, 'Female']);
$stmt->execute(['Bob', '12th', 91, 'Male']);
$dbo->commit();
echo "Records inserted successfully!";
} catch (PDOException $e) {
$dbo->rollBack();
echo "Error: " . $e->getMessage();
}
try {
$dbo->beginTransaction();
$stmt = $dbo->prepare("DELETE FROM student WHERE id = ?");
$stmt->execute([3]);
$dbo->commit();
echo "Student deleted!";
} catch (PDOException $e) {
$dbo->rollBack();
echo "Deletion failed: " . $e->getMessage();
}
try {
$dbo->beginTransaction();
$stmt = $dbo->prepare("UPDATE student SET mark = mark + 5 WHERE class = ?");
$stmt->execute(['12th']);
$dbo->commit();
echo "Marks updated!, No or rows updated: ".$stmt->rowCount();
} catch (PDOException $e) {
$dbo->rollBack();
echo "Failed to update marks: " . $e->getMessage();
}
DDL (Data Definition Language) | DML (Data Manipulation Language) |
---|---|
|
|
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.