I'm beginner to PHP transaction concept. My code is not working. I don't know what is my mistake. Anyone help me to find out my mistake.
My first query is wrong one. It didn't work. Second is successfully executed. If I have checked by IF condition, My control successfully moved to else part. It's fine. But My rollback function not working. Second query date will be present in table.
What is my mistake?
<?php
$link = mysqli_connect("localhost", "root", "", "hrms_db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Transaction start */
mysqli_begin_transaction($link, MYSQLI_TRANS_START_READ_WRITE);
/* disable autocommit */
mysqli_autocommit($link, FALSE);
$result1 = mysqli_query($link, "INSERT INTO EmployeeBackup (Name, OfficialEmail, Department, Manager_ID, MobileNO, Status, Location, full_name) value ('s', 's', '1' , '3', '5', '4', '5' , '78')");
$result2 = mysqli_query($link, "INSERT INTO hrms_general_master (lookup_type, lookup_description) value ('Testing', 'Testing')" );
if($result1 && $result2){
/* commit insert */
mysqli_commit($link);
echo "All queries were executed successfully";
} else{
/* Rollback */
mysqli_rollback($link);
echo "All queries were rolled back";
}
mysqli_close($link);
?>
Then please explain different type of parameter used in mysqli_begin_transaction
and use of it. I have little more doubt in mysqli_begin_transaction
and mysqli_commit
. Please clarify my doubt.
Thank you.