PHP's try...catch loops greatly simplify coding a module to handle most any MySQL transaction; here are the bare bones of it ...
<?php
function mysql_transaction( $conn, $aq ) {
$res = false;
while( !$res ) {
try {
mysqli_query( $conn, "start transaction" );
foreach( $aq as $q ) mysqli_query( $conn, $q );
$res = true;
}
catch( Exception $e ) {
echo $e->getMessage();
mysqli_query( $conn, "rollback" );
}
if( $res ) {
$res = mysqli_query( $conn, "commit" );
if( !$res ) mysqli_query( $conn, "rollback" );
}
if( !$res ) {
// retry code for when user so chooses, else break
}
}
return $res;
}
?>
Last updated 11 Mar 2025 |
 |