问题描述
<?php
$serverName = "localhost";
$username = "root";
$password = "";
$Database = "database";
$con = mysqli_connect($serverName, $username, $password, $Database);
if (mysqli_connect_errno()) {
echo "PLEASE IMPORT THE DATABASE";
}
try {
mysqli_autocommit($con,FALSE);
for ($i=1; $i < 100; $i++) {
$Query = "INSERT INTO `test`(`id`, `name`, `version`) VALUES ('".$i."','name".$i."','version".$i."')";
$Result = mysqli_query($con,$Query);
if($Result==false){
throw new Exception(" Error Found at index " . $i);
}
}
echo "calling commit";
mysqli_commit($con);
}
catch(Exception $e) {
echo "calling rollback";
mysqli_rollback($con);
echo 'Message: ' .$e->getMessage();
}
我已经创建了一个用于测试rollback()的测试文件.在测试表id
中作为主键.
I have created a test file for testing rollback(). In test table id
as primary key.
我第一次执行此文件,因此插入了99条记录(id从1到99).
First time i executed this file, so 99 records(id from 1 to 99) is inserted.
result :
calling commit
现在我从测试表中删除了pk值1,2,3.现在该表有96条记录(从4到99)如果我运行文件老化
Now i removed pk value 1,2,3 from test table. Now the table has 96 records(id from 4 to 99)if i run the file aging
result :
calling rollbackMessage: Error Found at index 4
但是,即使我调用rollback(),pk值1,2,3也会存储在表中.现在该表有99条记录(从1到99)为什么会这样呢?请帮助我.
But the pk value 1,2,3 is stored in the table even i call the rollback().Now the table has 99 records(id from 1 to 99)Why it is happening? Please help me.
推荐答案
检查您的数据库存储引擎.如果是MyISAM,请将其更改为InnoDB.
Check your Database Storage Engine. If it is MyISAM, change it into InnoDB.
这篇关于mysqli_rollback($ con)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!