我使用 pg_update() 来动态更新我的数据。
我有多个表要更新。如果任何查询失败,我想回滚。
我们可以使用带有静态查询的事务,但是有什么方法可以使用带有 pg_update() 的事务,因为我必须使用 pg_update() 来使用动态查询。
谢谢
最佳答案
我找到了解决问题的方法
pg_query("BEGIN") or die("Could not start transaction\n");
$dataToUpdate = array('name' => $name);;
$condition = array('id' => $id);
$res1 = pg_update($this->dbConnection, 'table_name1', $dataToUpdate, $condition);
$res2 = pg_update($this->dbConnection, 'table_name2', $dataToUpdate, $condition);
if ($res1 && $res2) {
pg_query("COMMIT") or die("Transaction commit failed\n");
} else {
pg_query("ROLLBACK") or die("Transaction rollback failed\n");;
}
关于php - 使用带有 pg_update() PHP 的事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50057669/