<?Php
try {
$my_conn->beginTransaction(); // Update a student's mark
$my_conn->exec("UPDATE student SET mark = 80 WHERE id = 1");
// Intentional error to trigger rollback
$my_conn->exec("UPDATE students SET mark = 90 WHERE id = 2");
$my_conn->commit();
} catch (Exception $e) {
$my_conn->rollBack();
echo "Failed: " . $e->getMessage();
}
?>
Explanation: If the second update fails (due to a typo in the table name), the entire transaction is rolled back, preserving the initial state.
<?Php
try {
$my_conn->beginTransaction(); // Check if a student's mark update maintains grade consistency
$newMark = 95;
if ($newMark <= 100) { $my_conn->exec("UPDATE student SET mark = $newMark WHERE id = 2"); }
else { throw new Exception("Invalid mark"); }
$my_conn->commit();
} catch (Exception $e) {
$my_conn->rollBack();
echo "Failed: " . $e->getMessage();
}
?>
Explanation: This ensures that updates adhere to the logical consistency rules defined (e.g., marks should not exceed 100).
try {
$my_conn->beginTransaction(); // Insert a new student record
// id 3 is alredy there , Integrity constraint violation
$my_conn->exec("INSERT INTO student (id,name, class, mark, gender)
VALUES (3,'Jane Doe', 'Six', 90, 'female')");
$my_conn->commit();
} catch (Exception $e) {
$my_conn->rollBack();
echo "Failed: " . $e->getMessage();
}
Explanation: This will say Integrity constraint violation: 19 UNIQUE constraint failed: student.id, student id 3 is already available.
<?Php
try {
$my_conn1 = new PDO('sqlite:my_student.db'); // Use correct path here, check config.php
$my_conn2 = new PDO('sqlite:my_student.db');
// Transaction 1: Update student's mark
$my_conn1->beginTransaction();
$my_conn1->exec("UPDATE student SET mark = 50 WHERE id = 3");
sleep(5); // Simulate delay before committing the transaction
// Transaction 2: Read student's mark during Transaction 1
$my_conn2->beginTransaction();
$mark = $my_conn2->query("SELECT mark FROM student WHERE id = 3")->fetchColumn();
echo "Mark in Transaction 2: $mark\n"; // Should return the original mark, not the updated one
$my_conn2->commit();
$my_conn1->commit(); // Transaction 1 commits after Transaction 2
} catch (Exception $e) {
$my_conn1->rollBack();
$my_conn2->rollBack();
echo "Failed: " . $e->getMessage();
}
?>
Explanation: While one transaction is updating the student's mark, another transaction checks the mark. Due to isolation, the second transaction sees the original mark until the first transaction is committed.
<?Php
try {
$my_conn->beginTransaction(); // Insert a new student record
$my_conn->exec("INSERT INTO student (name, class, mark, gender) VALUES ('Jane Doe', 'Six', 90, 'female')");
$my_conn->commit(); // Simulate a crash (uncomment the next line to simulate)
// exit();
} catch (Exception $e) {
$my_conn->rollBack();
echo "Failed: " . $e->getMessage();
}
?>
Explanation: Even if the system crashes right after the commit, the new student record remains in the database, ensuring durability.
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.