php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44341 PDOStatement::fetch() always returning string values
Submitted: 2008-03-05 18:47 UTC Modified: 2013-04-28 13:51 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: cortexd at wanadoo dot fr Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.2CVS-2008-03-05 (snap) OS: Windows XP
Private report: No CVE-ID: None
 [2008-03-05 18:47 UTC] cortexd at wanadoo dot fr
Description:
------------
PDOStatement::fetch() always returning string values, whatever the PDO::ATTR_STRINGIFY_FETCHES attribute value

HTTP Server : Apache/2.2.6 (Win32)
MySQL Server : 5.0.45-community-nt

Reproduce code:
---------------
<?php
$mysql = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$mysql->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);

var_dump($mysql->query('SELECT 42')->fetch(PDO::FETCH_NUM));
?>

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

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-09 17:26 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This only matters if the underlying driver tries to return native types. 
MySQL driver always returns strings.
 [2011-04-17 10:16 UTC] php_nospam at ramihyn dot sytes dot net
[quote]
This only matters if the underlying driver tries to return native types.
MySQL driver always returns strings.
[/quote]

3 years later: its still bugged in PHP 5.3.6. Shouldnt that be fixed by compiling with mysqlnd?
 [2011-04-17 10:36 UTC] php_nospam at ramihyn dot sytes dot net
Looking at the source of pdo_mysql shows, that pdo_mysql_stmt_get_col() returns a ZVAL reference instead of copying a string, if mysqlnd is in use. So why do the builds available for download still all have that bug?
 [2011-09-19 10:27 UTC] aphax91 at gmail dot com
"Thank you for taking the time to write to us, but this is not
a bug."

So you mean that when the value of a mySql int field is naturally returned as a string, this is not considered like a bug, is that a joke ? :)

Also, if you force the conversion of integers into strings, why the PDO driver PDO::ATTR_STRINGIFY_FETCHES isn't supported to let us the choice ? I don't understand the logic behind this...

Is it planned to fix this ?

Thanks.
 [2013-04-28 09:47 UTC] bulgur_wheat at hushmail dot com
Had to register a new email address just to comment on this. For the past 8 years, I've spent every day of my life coding in PHP. 

The manner in which this bug was dismissed is absolutely outrageous. Fix it.
 [2013-04-28 13:51 UTC] rasmus@php.net
bulgur_wheat at hushmail dot com can you explain what you think we should be 
fixing? As Ilia said years ago, PDO::ATTR_STRINGIFY_FETCHES only affects things 
if the underlying database driver is able to return native types. Most db drivers 
don't do this. They always return strings no matter what the column type is. We 
can't fix that aspect since we do not control these drivers.
 [2013-11-16 10:56 UTC] linzuxiong1988 at gmail dot com
I think it is a bug too , 
We can recode the msyql-driver code and compile to Fix it.

@  rasmus@php.net , Maybe you think php is leak type languange . 
But now we must compati the IOS client/ Android client , and so on ...

php-src/ext/pdo/pdo_mysql_driver.c  ?
 [2013-11-21 14:41 UTC] stephen at flowsa dot com
I had this issue this week and though I agree that it is not a PHP bug (but rather a driver issue) it is helpful to note that there is a solution.

I found that my version of PHP installed on Ubuntu was using the "libmysql" driver and not the "mysqlnd" driver. The "mysqlnd" driver will return number columns as number types.

I removed the old driver ("apt-get remove php5-mysql") and installed the "mysqlnd" driver ("apt-get install php5-mysqlnd") which solved the issue for me.

It's also important to ensure that PDO has these to settings set:
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Hopefully that is helpful to anyone who has the same issue.
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Fri Sep 21 14:01:27 2018 UTC