2

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.

Qirel
  • 25,449
  • 7
  • 45
  • 62
San Ka Ran
  • 156
  • 2
  • 18
  • http://stackoverflow.com/questions/12091971/how-to-start-and-end-transaction-in-mysqli may be helpful – Subin Thomas Sep 14 '15 at 12:04
  • 1
    Wow. thanks. I have wrongly set my search engine. Now I changed to InnoDB. Now it is working. But I have gotten one more doubt. Why transaction is working only in InnoDB.? – San Ka Ran Sep 14 '15 at 12:12
  • ya thanks subin thomas. I want to know flow of program execution. I read out more article. But still I have doubt on it. Please share some more details among the flow of transaction. – San Ka Ran Sep 14 '15 at 12:14
  • 2
    That's how mysql was created, only supports transactions for InnoDB, other engines may be faster but lack this very important functionality. – xception Sep 14 '15 at 12:33
  • ya thanks xception..!! – San Ka Ran Sep 15 '15 at 05:23

1 Answers1

3

You need the InnoDB access method to use transactions. The people who created MyISAM did not include transactions in their code.

Plenty of tutorials on the net explain DBMS transactions in general, and MySQL transactions in particular. Here is just one. http://www.tutorialspoint.com/mysql/mysql-transactions.htm

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Thanks Ollie jones!! I changed my access method into InnoDB. Now it is working fine. Once again thanks a lot. But still I am having little more doubt regarding this transaction concept. Please share some more details related to transaction. Jones.! – San Ka Ran Sep 15 '15 at 05:03