php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65287 PDOStatement::fetchAll wrong result
Submitted: 2013-07-18 14:14 UTC Modified: 2020-10-29 14:10 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:4 (100.0%)
Same OS:4 (100.0%)
From: ai at artem dot cc Assigned: nikic (profile)
Status: Closed Package: PDO MySQL
PHP Version: 5.6.16 OS: Centos
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ai at artem dot cc
New email:
PHP Version: OS:

 

 [2013-07-18 14:14 UTC] ai at artem dot cc
Description:
------------
When the mysqlnd driver is used PDOStatement->fetchAll(PDO::FETCH_COLUMN | 
PDO::FETCH_UNIQUE, 0) does not return proper results when 
PDO::ATTR_STRINGIFY_FETCHES and PDO::ATTR_EMULATE_PREPARES set to 0

Test script:
---------------
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', '123');
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, 0);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
var_dump($pdo->query("SELECT id FROM test")->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_UNIQUE, 0));



Expected result:
----------------
array(2) {
  [1]=>
  int(1)
  [2]=>
  int(2)
}

Actual result:
--------------
array(1) {
  [""]=>
  int(2)
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-01 12:30 UTC] felipe@php.net
-Package: PDO related +Package: PDO MySQL
 [2014-02-25 13:09 UTC] uw@php.net
-Status: Open +Status: Feedback
 [2014-02-25 13:09 UTC] uw@php.net
How would one support something for which there is not even any documentation... Apart from that it just works fine for me...


    $db = new PDO(  "mysql:host=127.0.0.1;port=3311;dbname=test", "root", "",
                    array(  PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                            PDO::ATTR_EMULATE_PREPARES   => false,
                            PDO::ATTR_STRINGIFY_FETCHES   => true,
                            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ) );


    $stmt = $db->query("SELECT 1, 1.23");
    var_dump($stmt->fetchAll());

    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
        $stmt = $db->query("SELECT 1, 1.23");
    var_dump($stmt->fetchAll());


array(1) {
  [0]=>
  array(2) {
    [1]=>
    string(1) "1"
    ["1.23"]=>
    string(4) "1.23"
  }
}
array(1) {
  [0]=>
  array(2) {
    [1]=>
    int(1)
    ["1.23"]=>
    string(4) "1.23"
  }
}


Please, provide a proper case that can be reproduced. Fetching results from an unknown table 'test' cannot be reproduced and verified.
 [2014-02-25 13:15 UTC] uw@php.net
-Package: PDO MySQL +Package: PDO related
 [2014-02-25 13:15 UTC] uw@php.net
nixnutz@linux-dstv:~/src/php-src> grep -i -R  stringi ext/pdo_*/*.c
nixnutz@linux-dstv:~/src/php-src> grep -i -R fetch_unique ext/pdo_*/*.c


AFAIK all this conversion stuff is core not driver stuff.
 [2014-02-25 14:30 UTC] ai at artem dot cc
-Status: Feedback +Status: Open
 [2014-02-25 14:30 UTC] ai at artem dot cc
Hi.

The problem appears in fetchAll method with PDO::FETCH_COLUMN | PDO::FETCH_UNIQUE, 0 as arguments. Try fetch results from any table with fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_UNIQUE, 0))
 [2015-11-27 07:50 UTC] mbeccati@php.net
-PHP Version: 5.4.17 +PHP Version: 5.6.16
 [2015-11-27 07:50 UTC] mbeccati@php.net
I've been able to replicate the issue on PHP 5.6, only when PDO_mysql is using mysqlnd. For completeness, I've also tried PDO_pgsql, which is working just fine.

Even if the test table has two different int records (4 and 5), I get:

array(1) {
  [""]=>
  int(5)
}

The PHP-7.0 or master branches do not seem to be affected.
 [2017-04-24 20:15 UTC] adambaratz@php.net
-Package: PDO related +Package: PDO MySQL
 [2017-04-24 20:15 UTC] adambaratz@php.net
Moving back to the PDO MySQL package based on Matteo's feedback.
 [2020-10-29 14:10 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-10-29 14:10 UTC] nikic@php.net
Per mbeccati's comment, this works fine in PHP 7, and PHP 5 is long since out of support. FWIW I also just tested this, and it still works fine.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC