[ACCEPTED]-How to get numeric types from MySQL using PDO?-pdo

Accepted answer
Score: 68

To answer your last question first, "yes," unfortunately 9 it's normal to receive numbers as strings. As 8 the manual quoted by Pascal says, mysqlnd 7 (PHP 5.3) will return native data types 6 from prepared statements, provided you turn 5 off the prepared statement emulation from 4 PDO.

new PDO($dsn, $user, $pass, array(


If you look at the bright side, it's 2 good practice to use prepared statements 1 anyway, so... ;)

Score: 41

I don't think having "numbers" can 18 be done in PHP 5.2 :-(

In PHP 5.3, it becomes 17 possible, if I remember correctly, when 16 you are using the new (new as in PHP >= 5.3) mysqlnd (MySQL Native Driver) driver.

Well, after 15 more digging through my bookmarks I found 14 this article about mysqlnd : PDO_MYSQLND: The new features of PDO_MYSQL in PHP 5.3

It says this 13 (quote) :

Advantages of using mysqlnd for PDO

mysqlnd returns native data types 12 when using Server-side Prepared Statements, for 11 example an INT column is returned as an 10 integer variable not as a string. That 9 means fewer data conversions internally.

But 8 this is PHP 5.3 only (provided your version 7 of PHP 5.3 is compiled with mysqlnd (and not old libmysql)), and 6 seems to only be the case for prepared statements 5 :-(


A solution would be to have, on 4 the PHP-side, a mapping-system (like an ORM -- see Doctrine ; just as an example of ORM : I don't know if it does what you're asking) to convert 3 results coming from the DB to PHP datatypes...

And 2 yes, this is bad if you want to use operators 1 like === and !==, which are type-sensitive...

Score: 31

Pascal's answer is correct. I had some trouble it making 16 it all work. Here is what you need to do.

First, make 15 sure you have mysqlnd installed and activated. Look 14 at php --info. In the pdo_mysql section, it should 13 look like:


PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.8-dev - 20102224 - $Revision: 321

If instead of seeing mysqlnd in the Client 12 API version, you see a line like...

Client API version => 5.5.29

...then 11 you don't have mysqlnd installed and going. Take 10 care of that first.

Second, PDO uses emulated 9 prepared statements by default for all MySQL connections. Ridiculous, but 8 there it is. And you will get native data 7 types only if you use real prepared statements 6 (called "server-side prepared statements" in 5 the linked blog post, which is now here). So 4 you must set PDO::ATTR_EMULATE_PREPARES 3 to false, like so:

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Lastly, make sure that 2 you have not set PDO::ATTR_STRINGIFY_FETCHES 1 to true.

$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
Score: 13

You might have mysqlnd installed, but it doesn't 7 mean it is ready for use by the regular 6 PDO extension (pdo_mysql), this is more often the case 5 on shared hosting, so make sure to enable 4 the nd_pdo_mysql extension, and also to disable other 3 extension pdo_mysql as it might create an conflict.


enter image description here

I 2 found this (duplicate) question, decided to post 1 my thoughts here, hope its fine ;)

More Related questions