PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)

后端 未结 7 2280
长发绾君心
长发绾君心 2020-11-21 06:03

I do know that PDO does not support multiple queries getting executed in one statement. I\'ve been Googleing and found few posts talking about PDO_MYSQL and PDO_MYSQLND.

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 06:38

    PDO does support this (as of 2020). Just do a query() call on a PDO object as usual, separating queries by ; and then nextRowset() to step to the next SELECT result, if you have multiple. Resultsets will be in the same order as the queries. Obviously think about the security implications - so don't accept user supplied queries, use parameters, etc. I use it with queries generated by code for example.

    $statement = $connection->query($query);
    do {
      $data[] = $statement->fetchAll(PDO::FETCH_ASSOC);
    } while ($statement->nextRowset());

提交回复
热议问题