Transactions - working but unsure about steps
| From: | Stuart Felenstein | Date: | Sat, 16 Oct 2004 12:52:32 +0000 |
| Subject: | Transactions - working but unsure about steps | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-36845@lists.php.net to get a copy of this message | ||
My statements are all working but I'm not sure if
things are set up correctly. I say this because at
one point the first $query failed, yet the rest of
inserts wre committed. Now I believe I need to set
autocommit to 0 , yet the query failed due to a syntax
error. Hence 0 records effected wouldn't be the case
here.
Here is what I have:
//Create these functions -
function begin()
{
mysql_query("BEGIN");
}
function commit()
{
mysql_query("COMMIT");
}
function rollback()
{
mysql_query("ROLLBACK");
}
connection statement with error checking...........
begin(); // transaction begins
$query = "INSERT INTO firsttable.....//first query
$result = mysql_query($query); // process first query
$query = "INSERT INTO secondtable...//second query
$result = mysql_query($query); // process second query
$query = "INSERT INTO thirdtable....//third query
$result = mysql_query($query); // process third query
then:
if(!$result)
{
echo mysql_errno($link) . ": " . mysql_error($link).
"\n";
rollback(); // transaction rolls back
exit;
}
else
{
commit(); // transaction is committed
echo "your insertion was successful";