|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 14:00:01 2025 UTC |
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.