简体   繁体   English

如何使用PHP PDO函数提取在SQL事务中获取特定查询的结果?

[英]How do you obtain the result of a particular query in a SQL transaction using PHP PDO function fetch?

I have a SQL transaction similar to the one below. 我有一个类似于下面的SQL事务。 I am unable to obtain the result of the 3rd query in the transaction using PHP function PDO::fetch() 我无法使用PHP函数PDO :: fetch()获取事务中的第三个查询结果

$database = DatabaseFactory::getFactory()->getConnection();
$sql = "BEGIN;
        DELETE FROM users WHERE ID = 4;
        UPDATE audit SET nousers= nousers - 1 WHERE ID = 4;
        SELECT nousers FROM audit WHERE ID = 4;
        COMMIT;";
$query = $database->prepare($sql);
$query->execute();

How do I use $query->fetch() to obtain the result of the 3rd query in the transaction? 如何使用$ query-> fetch()获取事务中第三个查询的结果?

You have five queries in your transaction, counting BEGIN and COMMIT . 您的交易中有五个查询,分别为BEGINCOMMIT

Call prepare() and execute() five times, one for each of the queries. 调用prepare()execute()五次,每个查询一次。 For your SELECT query, retrieve and examine the result set. 对于您的SELECT查询,检索并检查结果集。

There's no need to put all queries of a transaction into a single prepare / execute cycle. 无需将事务的所有查询放入单个prepare / execute周期。 The whole point of the transaction is to allow you to run multiple queries so they look like one unitary operation to other clients of the database server. 事务的整个重点是允许您运行多个查询,这样它们对于数据库服务器的其他客户端来说就像一个单一的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM