【问题标题】:Run Multiple Queries in a Transaction and Process Outputs Separately在事务中运行多个查询并分别处理输出
【发布时间】:2016-07-14 23:02:58
【问题描述】:

我可以在一个事务中运行多个查询并分别处理输出吗?我正在使用 mysql、mysqlpdo 和 php。例如,

try {
    $dbh->beginTransaction();

    // A set of queries; if one fails, an exception should be thrown
    $stmt1 = $dbh->prepare('first query');
    $stmt2 = $dbh->prepare('second query');
    $stmt3 = $dbh->prepare('third query');

    if ($stmt1->execute() && $stmt2->execute() && $stmt3->execute())
    {
        $dbh->commit();
    }

} catch(Exception $e) {
    $dbh->rollback();
}

// output data of each row
while ($rows = $stmt1->fetch(PDO::FETCH_ASSOC)) { ... }

// output data of each row
while ($rows = $stmt2->fetch(PDO::FETCH_ASSOC)) { ... }

// output data of each row
while ($rows = $stmt3->fetch(PDO::FETCH_ASSOC)) { ... }

查询产生不同的输出,这些输出将被不同地利用。

【问题讨论】:

    标签: php mysql pdo transactions


    【解决方案1】:

    对事务使用 SELECT 查询毫无意义。

    所以只需摆脱 begin 和 commit 的东西,只需选择您的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多