【发布时间】: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