使用pg_update()PHP的事务

时间:2018-04-27 07:42:15

标签: php postgresql pdo

我使用pg_update()动态更新我的数据。

我有多个表要更新。如果任何查询失败,我想回滚。

我们可以使用静态查询的事务但是有没有办法使用pg_update()进行事务,因为我必须使用pg_update()来使用动态查询。

由于

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法

  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");;
    }
相关问题