[thelist] Re: [PHP] MySql Rollback in PHP within a website

John W. Holmes holmes072000 at charter.net
Sun Jul 13 15:36:21 CDT 2003


bruce wrote:
> I have a question. I need to be able to try to perform a database update,
> but if it doesn't succeed, I need to be able to rollback the changes, and to
> inform the user that the changes didn't succeed.
> 
> I've looked at the MySql site, and can see somewhat how the Commit/RollBack
> functions work. However, I'm not sure how to create the required PHP code to
> implement this kind of process...
> 
> Basically I need to:
> 
> 	Create query...
> 	Perform Update on the table(s)
> 	Perform my check(s)
> 	If the checks fail
> 		alert user
> 		rollback updates
> 	else
> 		success
> 
> It's my understanding that I need to somehow set "START TRANSACTION" prior
> to beginning the update. But I'm not sure how to do that within the PHP
> code... Nor am I sure if there's something else I need to do...

$rs1 = mysql_query("START TRANSACTION");

$rs2 = mysql_query($query1);
$rs3 = mysql_query($query2);

if($error)
{ $rsx = mysql_query("ROLLBACK"); }
else
{ $rsx = mysql_query("COMMIT"); }

Use mysql_error() or mysql_errno() to see if an error occurred.

-- 
---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

PHP|Architect: A magazine for PHP Professionals – www.phparch.com






More information about the thelist mailing list