Home > Database > Mysql Tutorial > How Can I Retrieve Multiple Result Sets from Multiple Queries Using PDO_MYSQLND?

How Can I Retrieve Multiple Result Sets from Multiple Queries Using PDO_MYSQLND?

DDD
Release: 2024-11-20 04:11:02
Original
955 people have browsed it

How Can I Retrieve Multiple Result Sets from Multiple Queries Using PDO_MYSQLND?

Executing Multiple Queries with PDO

In PHP version 5.3, the PDO_MYSQL driver was replaced with PDO_MYSQLND, which introduced support for multiple queries. However, obtaining multiple result sets from these queries can be confusing.

Challenge:

Executing multiple SELECT queries using PDO_MYSQLND's query() method only returns the first result set. For example:

$db->query("SELECT 1; SELECT 2;")->fetchAll(PDO::FETCH_ASSOC);
Copy after login

This code will only return the result of the first query, which is:

array(1) {
  [0]=>
  array(1) {
    [1]=>
    string(1) "1"
  }
}
Copy after login

Solution:

To retrieve subsequent result sets, you must use the PDOStatement::nextRowset() method.

$stmt = $db->query("SELECT 1; SELECT 2;");
$stmt->nextRowset();
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
Copy after login

This will return the result of the second query.

Implementation:

This implementation may seem a bit peculiar since it would be more convenient if multiple queries returned all results under one array. However, this design allows for fetching each result set using different fetch styles.

The above is the detailed content of How Can I Retrieve Multiple Result Sets from Multiple Queries Using PDO_MYSQLND?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template