我有3个名为$stmt9
,$stmt10
和$stmt11
的查询。我需要按顺序运行查询,例如运行9
,然后10
,最后11
。如果出现问题,我想回滚已经完成的查询,例如如果问题出现在10
上,则应该回滚9
,或者如果在11
上,则应回滚10
和9
。
我有以下代码,但是当出现问题时,有时它仍然仅运行$stmt9
,或$stmt9
和$stmt10
,而不是$stmt11
。
如何解决?
这是更新操作:
$this->conn->beginTransaction();
$stmt9 = $this->conn->prepare("UPDATE tableONE SET condition= :condition WHERE `tb1_id`= :id LIMIT 1");
condition
$id = Check_Get_Param($_GET['id']);
$stmt9->bindParam(':condition' , $this->condition);
$stmt9->bindParam(':id' , $id, PDO::PARAM_INT);
$stmt9->execute();
$stmt10 = $this->conn->prepare("UPDATE tableTOW SET disterbute = :dis WHERE tb2_id = :tbl2id LIMIT 1");
$stmt10->bindParam(":counting", $this->counting, PDO::PARAM_INT);
$stmt10->bindParam(":tbl2id ", $this->tbl2id , PDO::PARAM_INT);
$stmt10->execute();
$stmt11 = $this->conn->prepare("DELETE FROM tableTHREE WHERE tb3_id = :id limit 1");
$stmt11->bindParam(":id",$id, PDO::PARAM_INT);
$stmt11->execute();
if($stmt9 && $stmt10 && $stmt11){
$this->conn->commit(); //This will save my changes
header('location:../success.php');
exit;
} else {
$this->conn->rollBack(); //This will undo your changes
echo '<meta http-equiv="refresh" content="0">'. $message;
}
}
}
答案 0 :(得分:0)
您正在寻找的是一项交易。 我查了一下,它是在pdo中实现的。
取自文件的例子:
<?php
/* Begin a transaction, turning off autocommit */
$dbh->beginTransaction();
/* Change the database schema and data */
$sth = $dbh->exec("DROP TABLE fruit");
$sth = $dbh->exec("UPDATE dessert
SET name = 'hamburger'");
/* Recognize mistake and roll back changes */
$dbh->rollBack();
/* Database connection is now back in autocommit mode */
?>