Dear All,
I am trying to apply transaction into my codes. Below is what I have done. It works fine. I would just like to know incase is there something wrong or missing in my way ? Thank you. What is different between mysql and mysqli ?
$rollbackStatus=False;
$link = mysql_connect(dbHost, dbUser, dbPassword);
if(!$link)
{
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(dbDatabase);
if(!$db)
{
die("Unable to select database");
}
mysqli_autocommit($link, FALSE);
$selectQuery1 ="Select clientName From tblClient Where clientName='".$clientName."'";
$result1 = mysql_query($selectQuery1);
if ($result1 !== TRUE)
{
mysqli_rollback($link);
$rollbackStatus=True;
}
$row1 = mysql_fetch_array($result1, MYSQL_ASSOC);
$n1 = mysql_num_rows($result1);
if($n1>0)
{
$status="<span class=\"statusFailed\">: Client ".$clientName." Alread Exist.</span>";
}
else
{
$insertQuery1 = "Insert Into tblClient Set ".
"clientName='".$clientName."', ".
"clientContactPerson='".$clientContactPerson."',".
"clientAddress='".$clientAddress."',".
"clientContactNumber='".$clientContactNumber."',".
"clientStatus='a'";
$result2 = mysql_query($insertQuery1,$link);
if ($result2 !== TRUE)
{
$status="<span class=\"statusFailed\">: Error adding adding Client:" . mysql_error() . "</span>";
mysqli_rollback($link);
$rollbackStatus=True;
}
else
{
$status="<span class=\"statusOk\">: Client ".$clientName." Added Succesfully.</span>";
}
}
if($rollbackStatus=="False")
{
mysqli_commit($link);
}
mysqli_close($link);
}