RE: [PHP] transactions

From: Date: Mon, 29 Mar 2004 18:28:53 +0000
Subject: RE: [PHP] transactions
Groups: php.general 
Request: Send a blank email to php-general+get-181686@lists.php.net to get a copy of this message
Matthew Oatham wrote: > Hi, > > Is there an elegant way to recover from DB errors in MySQL using PHP, > i.e. transactions and rolling back - basically I have an insert > statement then an update statement. if the insert succeeds the update > is run but if the update fails I want to undo the insert! > > Any suggestions, I guess I could get the last inserted row id and > perform an sql delete but is there a more elegant way? > > Cheers > > Matt Though I know there are many people out there who cringe at the thought of using DB abstraction layers, I really like ADOdb and it has very nice transaction support built in (as long as the underlying database supports it, obviously). $conn->BeginTrans(); $commit = false; $query = 'select col1, col2 from table 1'; $rs = $conn->Execute($query) ? true : false; $commit = $rs ? true : false; $query = 'insert into table1 (col1, col2) values (col1, col2)'; $commit = $commit == true && $conn->Execute($query) ? true : false; $query = 'insert into table2 (col1, col2) values (col1, col2)'; $commit = $commit == true && $conn->Execute($query) ? true : false; $query = 'insert intod table3 (col1, col2) values (col1, col2)'; $commit = $commit == true && $conn->Execute($query) ? true : false; $commit == true ? $conn->BeginTrans() : $conn->RollbackTrans(); None of the above will be committed since there is a syntax error in the third query. HTH. Pablo

« previous php.general (#181686) next »