Php pdo rollBack()不适用于update语句
我使用PHP PDO事务。我有insert和update语句,我想在rowCount()返回0时回滚事务。请参阅下面的示例代码Php pdo rollBack()不适用于update语句,php,mysql,pdo,transactions,rollback,Php,Mysql,Pdo,Transactions,Rollback,我使用PHP PDO事务。我有insert和update语句,我想在rowCount()返回0时回滚事务。请参阅下面的示例代码 <?php $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '', array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false )); $userId
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
));
$userIds = [1,2,""];
$paymentAmounts = [10.50,20.50,30.50];
$error = array();
$pdo->beginTransaction();
try
{
foreach($userIds as $key => $userId )
{
$sql = "INSERT INTO payments (user_id, amount) VALUES (?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
$userId,
$paymentAmounts[$key]
)
);
if($stmt->rowCount() <= 0)
{
$error[] = ["Error occurred in userId: ".$userId];
}
$sql = "UPDATE users SET credit = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
$paymentAmounts[$key],
$userId
)
);
if($stmt->rowCount() <= 0)
{
$error[] = ["Error occurred in userId: ".$userId];
}
}
if(!empty($error))
{
$displayError = implode(",", $error);
throw new Exception($displayError);
}
// It should not reach here, if there is an error.
$pdo->commit();
}
catch(Exception $e){
echo $e->getMessage(); // displayError
$pdo->rollBack(); // <<<==== It should rollback both Insert and Update to original state
}
?>
测试后#1
测试#2~故意导致异常-使用以下数据的数组长度不匹配:
$userIds = array(1,2,3,4,5,6);
$paymentAmounts = array( 25,30,55,30,45,44,21 );/* <----- extra item to cause exception */
与以前相同的数据-异常后查询未提交到db
测试#3-伪造列名的查询
$sql = "update users set credit = boomerang + ? where id = ?";/* <----- ficticious column cause exception */
同样,快速检查显示数据没有变化,因此整个过程看起来都正常运行您的代码应该包括一个表创建查询和一个插入查询来填充它,这样每个人都可以确认该行为。不过,这是一个疯狂的猜测。你的用户表引擎真的是InnoDB吗?是的,它是InnoDB。谢谢你的回复。我已经添加了表和插入查询。你应该提供打算一次运行的完整代码。让一个人只是复制粘贴它,然后得到结果。但是请确保您事先自己运行它,它实际上支持您的说法,即某些表没有回滚,这是为了抛出一个异常——一对故意不匹配的数组。我只是在我这边进行了测试。更新仍然不会还原为原始数据。你能换这条线吗?From$sql=“updateusers set credit=credit+,其中id=?”$paymentAmounts=数组(25,30,55,30,45,44,21);To$sql=“更新用户设置信用=?其中id=?”$付款金额=数组(40,90,15,30,45,44,21);
mysql> select * from payments;
Empty set (0.00 sec)
mysql> select * from users;
+------+--------+
| id | credit |
+------+--------+
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
+------+--------+
6 rows in set (0.00 sec)
mysql> select * from payments;
+----+---------+--------+
| id | user_id | amount |
+----+---------+--------+
| 1 | 1 | 25 |
| 2 | 2 | 30 |
| 3 | 3 | 55 |
| 4 | 4 | 30 |
| 5 | 5 | 45 |
| 6 | 6 | 44 |
+----+---------+--------+
6 rows in set (0.00 sec)
mysql> select * from users;
+------+--------+
| id | credit |
+------+--------+
| 1 | 25 |
| 2 | 30 |
| 3 | 55 |
| 4 | 30 |
| 5 | 45 |
| 6 | 44 |
+------+--------+
7 rows in set (0.00 sec)
$userIds = array(1,2,3,4,5,6);
$paymentAmounts = array( 25,30,55,30,45,44,21 );/* <----- extra item to cause exception */
Exception Raised: Unbalanced source arrays: $userids:6, $paymentAmounts:7
mysql> select * from users;
+----+--------+
| id | credit |
+----+--------+
| 1 | 25 |
| 2 | 30 |
| 3 | 55 |
| 4 | 30 |
| 5 | 45 |
| 6 | 44 |
+----+--------+
6 rows in set (0.00 sec)
$sql = "update users set credit = boomerang + ? where id = ?";/* <----- ficticious column cause exception */
Exception Raised: Query #2 -> Error occurred in userId: 1 count: 0
mysql> select * from users;
+----+--------+
| id | credit |
+----+--------+
| 1 | 25 |
| 2 | 30 |
| 3 | 55 |
| 4 | 30 |
| 5 | 45 |
| 6 | 44 |
+----+--------+
6 rows in set (0.00 sec)