14

As of PHP version 5.3 PDO_MYSQL driver has been repleaced in favour of PDO_MYSQLND. It introduced support for multiple queries.

Though, I can't figure out how to get both result sets if more than one SELECT query has been passed. Both queries have been executed, it can't be that the second one was just dumped.

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

Returns:

array(1) {
  [0]=>
  array(1) {
    [1]=>
    string(1) "1"
  }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Gajus
  • 69,002
  • 70
  • 275
  • 438
  • possible duplicate of [PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)](http://stackoverflow.com/questions/6346674/pdo-support-for-multiple-queries-pdo-mysql-pdo-mysqlnd) – Francis Avila Jun 30 '12 at 05:25
  • @FrancisAvila That is also my question and not a duplicate. I found the answer. Will post in a second. – Gajus Jun 30 '12 at 05:26

1 Answers1

24

It turns out that you need to use PDOStatement::nextRowset.

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

This will return result for the second query.

It is a bit odd implementation. It would certainly be easier if multi-query statement would just return both results sets under one array. However, the advantage is that this implementation allows to fetch every query using different FETCH styles.

Gajus
  • 69,002
  • 70
  • 275
  • 438