PDO MS SQL Server and PDO MySQL both return an array of strings when you fetch, even if the SQL type of the columns were meant to be numeric types such as int or float. I was managed to work around it, but I want to understand why they were designed this way to begin with. Is it because PDO itself don't support it (or didn't support it before)?
2 Answers
With PDO MySQL the underlying C API returns a structure of strings when calling mysql_fetch_row(). Since PHP is a loosely typed language that automatically casts to integer as necessary, I suspect the PDO developers chose to return them as-is. This would be faster than looking up each column type and dynamically casting to an integer / float.
Specifically regarding float, a native PHP float may be different than the original stored value when converted from a string. For example, MySQL columns support a precision while PHP floats do not. Leaving floats as a string from the database allows you more control (e.g. choosing to use a library with different precision).
1 Comment
because PDO itself don't support it (or didn't support it before)?
Pretty much yes but for mydsql it is not because PDO but because mysql C API.
Since it changed, for the moment PDO can return correct datatypes if you run a native prepared statement against a mysqlnd-based PDO instance.
$var = 10;and$var = "10";(an integer and a string, both assigned to 10) are both treated as numbers.