I have many select insert update query , i have written transaction query but want to know if its safe or some need to be corrected.
Also if all is ok i get successful "Transaction completed successfully!" , but if something get wrong then no error message soon , hence confuse its working or not.
Will $result_a
and $result_b
will be rolled backed if $result_Cb
is FAIL or any other and vise-verse?
Below is what I have tried. Please let me know if something is wrong:
try {
/* switch autocommit status to FALSE. Actually, it starts transaction */
global $db;
mysqli_autocommit($db, FALSE);
$sqla ="INSERT INTO A () VALUES () ";
$result_a = mysqli_query($db,$sqla);
if($result_a){
$sqlb ="INSERT INTO B () VALUES () ";
$result_b = mysqli_query($db,$sqlb);
if($result_b){
$sqlCb ="UPDATE CB () SET ... ";
$result_Cb = mysqli_query($db,$sqlCb);
} else { echo "ERROR" ; }
} else {
echo "ERROR" ;
}
mysqli_commit($db);
echo 'Transaction completed successfully!';
} catch (Exception $e) {
echo 'Transaction failed: ' . $e->display_notification_centered(_("transaction rolled back"));
mysqli_rollback($db);
}
/* switch back autocommit status */
mysqli_autocommit($db, TRUE);
Above is just simple insert update , one of my page contain
SELECT >> INSERT >> UPADATE >> SELECT >> INSERT ...
hence please let me know if something went wrong .. I am safe with my CORRECT database values.
EDIT : 14-02-2014 ( add MY MODIFIED CODE BELOW as i was facing some problem in rollback at some pages )
function autocommitfalse(){
global $db;
/* switch autocommit status to FALSE. Actually, it starts transaction */
return mysqli_autocommit($db, FALSE);
}
function autocommittrue(){
global $db;
return mysqli_autocommit($db, TRUE);
}
function commitquery(){
global $db;
return mysqli_commit($db);
}
function rollbackedquery(){
global $db;
return mysqli_rollback($db);
}
===============================================================================
try {
autocommitfalse();
$SQLSELECT = "SELECT * FROM BC ";
$result_as = mysqli_query($db,$SQLSELECT);
if(!$result_as ){
throw new Exception('Wrong SQL SELECT: ' . $SQLSELECT_a. ' Error: '.db_error_msg($db) . db_error_no());
}
$sqla ="INSERT INTO A () VALUES () ";
$result_a = mysqli_query($db,$sqla);
if(!$result_a){
throw new Exception('Wrong SQL SELECT: ' . $result_a. ' Error: '.db_error_msg($db) . db_error_no());
}
$sqlb ="INSERT INTO B () VALUES () ";
$result_b =mysqli_query($db,$sqlb);
if(!$result_b){
throw new Exception('Wrong SQL SELECT: ' . $result_a. ' Error: '.db_error_msg($db) . db_error_no());
}
$sqlCb ="UPDATE CB () SET ... ";
$result_Cb = mysqli_query($db,$sqlCb);
if($result_Cb === false && mysqli_affected_rows($db) == 0 ){
throw new Exception('Wrong SQL SELECT: ' . $result_a. ' Error: '.db_error_msg($db) . db_error_no());
}
commitquery();
echo 'Transaction completed successfully!';
} catch (Exception $e) {
echo"<br \>";
echo "<table align=center><tr><td>";
echo $e->getMessage();
echo "</td></tr></table>";
echo"<br \>";
echo display_error(_("Transaction failed: transaction rolled back"));
rollbackedquery();
}
autocommittrue();
If something wrong ..please let me know with change in it ( above code ) ..so that i am more clear .... as i have many if and but query and in my some page i get rollback but some pages its not. :(