I've seen this question repeated a few times on Stack Overflow but none sufficiently explore the problem (or at least in a way that is helpful to me)
The problem is that a DB query should return integer data types in PHP for integer columns. Instead the query returns every column as a string type.
I've ensured that "PDO::ATTR_STRINGIFY_FETCHES" if false just to make sure results aren't being cast to string.
Answers that I've seen:
From my research I understand that this is a driver implementation issue.
Many sources claim that the MySQL native driver does not support returning numeric types. This doesn't seem true since it works on Mac OS X. Unless they mean to say that "the MySQL native driver on Linux doesn't support the feature".
This implies that there is something special about the driver/environment I have installed on Mac OS X. I've been trying to identify the differences in order to apply a fix but I'm limited by my knowledge of how to check these things.
php -i
pdo_mysql
PDO Driver for MySQL => enabled Client API version => 5.1.72
php -i
pdo_mysql
PDO Driver for MySQL => enabled Client API version => mysqlnd 5.0.10 - 20111026 - $Id: e707c415db32080b3752b232487a435ee0372157 $
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
Any help and expertise would be appreciated :) I'll definitely be posting back here if I find the answer.
The solution is to ensure that you are using the mysqlnd driver for php.
When viewing php -i
, there will be no mention of "mysqlnd". The pdo_mysql
section will have something like this:
pdo_mysql
PDO Driver for MySQL => enabled Client API version => 5.1.72
Most installation guides for L/A/M/P suggest apt-get install php5-mysql
but the native driver for MySQL is installed by a different package: php5-mysqlnd
. I found that this was available with the ppa:ondrej/php5-oldstable.
To switch to the new driver (on Ubuntu):
apt-get remove php5-mysql
apt-get install php5-mysqlnd
service apache2 restart
Now php -i
will mention "mysqlnd" explicitly in the pdo_mysql
section:
pdo_mysql
PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.10 - 20111026 - $Id: e707c415db32080b3752b232487a435ee0372157 $
Ensure that PDO::ATTR_EMULATE_PREPARES
is false
(check your defaults or set it):
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
Ensure that PDO::ATTR_STRINGIFY_FETCHES
is false
(check your defaults or set it):
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
† BIGINTs with a value greater than a 64 bit signed int (9223372036854775807) will return as a string (or 32 bits on a 32 bit system)
object(stdClass)[915]
public 'integer_col' => int 1
public 'double_col' => float 1.55
public 'float_col' => float 1.5
public 'decimal_col' => string '1.20' (length=4)
public 'bigint_col' => string '18446744073709551615' (length=20)