withinweb

Information and support for products of withinweb.com

withinweb header image 2

Simple transaction example using PHP and mysql_query

July 30th, 2012 · No Comments

<?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.

Tags: General PHP · SQL databases

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.