<?php
// A simple transaction example using PHP and mysql_query
//——————————————–
@mysql_connect(“localhost”,”myusername”, “mypassword”) or die(mysql_error());
@mysql_select_db(“mydatabase”) or die(mysql_error());
$query = “INSERT INTO mytable (firstname, lastname) values (‘Fred’,’Bloggs’)”;
begin(); // begin transaction
$result = @mysql_query($query);
if(!$result) {
rollback(); // failed so roll back transaction
echo “Rolled back”;
exit;
}
else
{
commit(); // Committ transaction
echo “Transaction completed”;
}
//——————————————–
//——————————————–
function begin() {
@mysql_query(“BEGIN”);
}
//——————————————–
function commit() {
@mysql_query(“COMMIT”);
}
//——————————————–
function rollback() {
@mysql_query(“ROLLBACK”);
}
?>
The above code illustrates the use of transactions with mysql_query code.