简体   繁体   English

交易不适用于php,mysql

[英]Transaction Does not working in php,mysql

I was just trying transaction with php. 我只是想用php进行交易。 But does not working. 但是不起作用。 here is the code 这是代码

$conn = mysql_connect('localhost', 'root', '') or die (mysql_error());
mysql_select_db('users',$conn) or die (mysql_error());
$sql = 'INSERT INTO `login` (id ,username ,password ,isactive)VALUES (3,  "rtyer",  "edt6e4",  "y");';
$sql1 = 'INSERT INTO `user` (
            `f_name` ,
            `l_name` ,
            `age` ,
            `place` ,
            `loginid`
            )
            VALUES (
            3,  "dsgdhg",  "yuity",  45,  "fj",  3
            );';
mysql_query("SET autocommit = off");
mysql_query("BEGIN");
mysql_query("START TRANSACTION");
$result1 = mysql_query( $sql, $conn );
$result2 = mysql_query( $sql1, $conn );
if(!$result2)
{
mysql_query("ROLLBACK");
echo "transaction rolled back";
exit;
}
else
{
mysql_query("COMMIT");
echo "Database transaction was successful";
}
mysql_close($conn);


there is mistake in the sql1 so data is not inserting into user table but rollbacking from the login table. sql1中有错误,因此数据不是插入user表中,而是从login表回滚。 Its entering into the if block but rollback is not occuring. 它进入if块,但不会发生回滚。 Whats the problem here? 这里有什么问题?

Delete the extra semi-colon at this line and use double quotes and single quotes on values: 删除此行多余的分号,并在值上使用双引号和单引号:

$sql = 'INSERT INTO login (id ,username ,password ,isactive)VALUES (3, "rtyer", "edt6e4", "y");'; $sql = 'INSERT INTO登录(id ,username ,password ,isactive)VALUES (3, "rtyer", "edt6e4", "y");'; <--- this part <---这部分

Into this and do the same to $sql1 进入这个并对$ sql1做同样的事情

$sql = "INSERT INTO login (id ,username ,password ,isactive)VALUES ('3', 'rtyer', 'edt6e4', 'y')"; <-- deleted semi-colon <-删除分号

$sql1 = "INSERT INTO `user` (
            `f_name` ,
            `l_name` ,
            `age` ,
            `place` ,
            `loginid`
            )
            VALUES (
            '3',  'dsgdhg',  'yuity',  '45',  'fj',  '3'
            )";

Transaction will work only in InnoDB. 交易仅在InnoDB中有效。 here it was MyISAM. 这是MyISAM。

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

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