// Open connection to database $conn = new mysqli($servername, $username, $password, $dbname); // Start transaction $conn->beginWork(); // Insert data into table $sql = "INSERT INTO customers (name, email, phone) VALUES ('John Doe', 'johndoe@example.com', '555-1234')"; $conn->query($sql); // Update data in table $sql = "UPDATE customers SET phone='555-4321' WHERE id=1"; $conn->query($sql); // Commit changes $conn->commit();
// Open connection to database $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // Start transaction $conn->beginTransaction(); // Delete data from table $sql = "DELETE FROM orders WHERE customer_id=1"; $conn->exec($sql); // Insert new data into table $sql = "INSERT INTO orders (customer_id, product_name, price) VALUES (1, 'Widget', 9.99)"; $conn->exec($sql); // Commit changes $conn->commit();In this example, we use the PDO library to connect to the database and start a transaction. We then delete all orders for a specific customer and insert a new order. Again, if either of these queries fail, the changes will be rolled back and not saved to the database. The package library for these examples is either mysqli or PDO, depending on which connection method is used.