I'm using PDO and MySQL, for some reason when getting values from the database that are int type, the PDOStatement is returning a string representation of the number and not a value of numeric type. How do I prevent this from happening?
I noticed there is a attribute of the PDO class: PDO::ATTR_STRINGIFY_FETCHES
that is supposed to take care of this but, when trying to modify it, it throws an error saying the attribute is not valid for MySQL driver.
Is it normal to get strings instead of numbers when consulting a database?
I don't think having "numbers" can be done in PHP 5.2 :-(
In PHP 5.3, it becomes possible, if I remember correctly, when you are using the new (new as in PHP >= 5.3) mysqlnd (MySQL Native Driver) driver.
Well, after more digging through my bookmarks I found this article about mysqlnd :
PDO_MYSQLND: The new features of PDO_MYSQL in PHP 5.3
It says this (quote) :
But this is PHP 5.3 only (provided your version of PHP 5.3 is compiled with mysqlnd (and not old libmysql)), and seems to only be the case for prepared statements :-(
Sorry...
A solution would be to have, on 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 results coming from the DB to PHP datatypes...
And yes, this is bad if you want to use operators like
===
and!==
, which are type-sensitive...