简体   繁体   English

事务无法正常工作-PHP和MySql

[英]Transaction is not working - PHP and MySql

I'm beginner to PHP transaction concept. 我是PHP交易概念的初学者。 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. 如果我已通过IF条件进行检查,则我的控件已成功移至其他部分。 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. 然后,请解释在mysqli_begin_transaction使用的不同类型的参数及其用法。 I have little more doubt in mysqli_begin_transaction and mysqli_commit . 我对mysqli_begin_transactionmysqli_commit毫无疑问。 Please clarify my doubt. 请澄清我的疑问。

Thank you. 谢谢。

You need the InnoDB access method to use transactions. 您需要使用InnoDB访问方法来使用事务。 The people who created MyISAM did not include transactions in their code. 创建MyISAM的人的代码中未包含事务。

Plenty of tutorials on the net explain DBMS transactions in general, and MySQL transactions in particular. 网上的大量教程通常介绍DBMS事务,尤其是MySQL事务。 Here is just one. 这只是一个。 http://www.tutorialspoint.com/mysql/mysql-transactions.htm http://www.tutorialspoint.com/mysql/mysql-transactions.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM