For the records, PDO provides a feature that would alter this, PDO::ATTR_STRINGIFY_FETCHES:
Convert numeric values to strings when fetching. Requires bool.
The rationale behind the setting is that database engines can handle very large numbers (Oracle, for instance, allows 38-digit numbers) and PHP cannot. Retrieving those numbers as strings is a method to keep safe and prevent data loss.
Unluckily, the MySQL driver does not support it:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'test', 'test');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$sql = 'SELECT 1 AS integer_number, 3.14 AS floating_point';
$res = $pdo->query($sql);
while($row = $res->fetch(PDO::FETCH_ASSOC)){
var_dump($row);
}
array(2) {
["integer_number"]=>
string(1) "1"
["floating_point"]=>
string(4) "3.14"
}