php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63813 FETCH_GROUP Does not work with FETCH_KEY_PAIR
Submitted: 2012-12-20 07:56 UTC Modified: 2012-12-23 18:15 UTC
From: contact at joycebabu dot com Assigned:
Status: Not a bug Package: PDO related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2012-12-20 07:56 UTC] contact at joycebabu dot com
Description:
------------
Documentation for PDO::FETCH_GROUP says

"Group return by values. Usually combined with PDO::FETCH_COLUMN or 
PDO::FETCH_KEY_PAIR."

But FETCH_GROUP does not work with FETCH_KEY_PAIR. When three columns are 
specified in the query, the fetch fails with the erorr message

"Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: 
PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain extactly 2 
columns."

When 2 columns are specified in the query, the fetch returns an associative array 
with only one value for each unique grouped key, which is useless.

Test script:
---------------
/*
employee table
--------------------------------
| location | emp_id | name     |
--------------------------------
| New York |      1 |     John |
| New York |      2 |     Jane |
| London   |      3 |     Jack |
| London   |      4 |     Nick |
--------------------------------
*/

$db->query('SELECT location, emp_id, name FROM employees');
$employees = $db->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_KEY_PAIR);


Expected result:
----------------
array(
   'New York' => array(
      1 => 'John',
      2 => 'Jane'
   ),
   'London' => array(
      3 => 'Jack',
      4 => 'Nick'
   )
)


Actual result:
--------------
array(
	'New York' => 'Jane', 
	'London' => 'Nick'
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-23 18:15 UTC] felipe@php.net
-Status: Open +Status: Not a bug
 [2012-12-23 18:15 UTC] felipe@php.net
Your query must be only "SELECT location, name FROM employees"
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 23:01:24 2019 UTC