$connection->begin_transaction(); // Start transaction
$connection->commit(); // Commit the transaction
$connection->rollback(); // Roll back if there is an error
try {
$connection->begin_transaction();
$stmt = $connection->prepare("UPDATE student SET mark = mark + 10 WHERE id = ?");
$stmt->bind_param("i", $id);
$id = 1;
$stmt->execute();
$connection->commit();
echo "Number of records updated: " . $stmt->affected_rows;
} catch (Exception $e) {
$connection->rollback();
echo "Transaction failed: " . $e->getMessage();
}
$connection->query("SAVEPOINT savepoint1");
// Perform additional operations
$connection->query("ROLLBACK TO savepoint1"); // Roll back to the savepoint
$connection->commit(); // Commit the final changes
try {
$connection->begin_transaction();
$stmt = $connection->prepare("INSERT INTO student (name, class, mark, gender) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssis", $name, $class, $mark, $gender);
$name = 'Alice'; $class = 'Five'; $mark = 88; $gender = 'Female';
$stmt->execute();
echo "Number of records added : " . $stmt->affected_rows;
echo "<br>Insert ID : ".$connection->insert_id;
$name = 'Bob'; $class = 'Three'; $mark = 91; $gender = 'Male';
$stmt->execute();
echo "<br>Number of records added : " . $stmt->affected_rows;
echo "<br>Insert ID : ".$connection->insert_id;
$connection->commit();
echo "<br>Records inserted successfully!";
} catch (Exception $e) {
$connection->rollback();
echo "Transaction failed: " . $e->getMessage();
}
try {
$connection->begin_transaction();
$stmt = $connection->prepare("UPDATE student SET mark = mark + 5 WHERE class = ?");
$stmt->bind_param("s", $class);
$class = '10th';
$stmt->execute();
$class = '12th';
$stmt->execute();
$connection->commit();
echo "Marks updated successfully!";
} catch (Exception $e) {
$connection->rollback();
echo "Transaction failed: " . $e->getMessage();
}
try {
$connection->begin_transaction();
$stmt = $connection->prepare("DELETE FROM student WHERE id = ?");
$stmt->bind_param("i", $id);
$id = 3;
$stmt->execute();
echo "<br> Number of records deleted : " . $stmt->affected_rows;
$connection->commit();
echo "Record deleted successfully!";
} catch (Exception $e) {
$connection->rollback();
echo "Failed to delete record: " . $e->getMessage();
}
try {
$connection->begin_transaction();
// First operation
$connection->query("INSERT INTO student (name, class, mark, gender)
VALUES ('John', '8th', 90, 'Male')");
$connection->query("SAVEPOINT savepoint1");
// Second operation, which might fail
$connection->query("INSERT INTO student (name, class, mark, gender)
VALUES ('Jane', '9th', 85, 'Female')");
// If something fails, rollback to savepoint1
$connection->query("ROLLBACK TO savepoint1");
// Commit the rest of the transaction
$connection->commit();
} catch (Exception $e) {
$connection->rollback();
echo "Transaction failed: " . $e->getMessage();
}
| Type of Operation | Definition | Transactional Behavior |
|---|---|---|
| DDL (Data Definition Language) | Includes commands like `CREATE`, `ALTER`, and `DROP` that define or modify database structures. |
|
| DML (Data Manipulation Language) | Includes commands like `INSERT`, `UPDATE`, and `DELETE` that manipulate data within tables. |
|

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.