Edit report at https://bugs.php.net/bug.php?id=63813&edit=1

 ID:                 63813
 Updated by:         fel...@php.net
 Reported by:        contact at joycebabu dot com
 Summary:            FETCH_GROUP Does not work with FETCH_KEY_PAIR
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            PDO related
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Your query must be only "SELECT location, name FROM employees"


Previous Comments:
------------------------------------------------------------------------
[2012-12-20 07:56:36] 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'
)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63813&edit=1

Reply via email to