Laravel 5.0 PDO Querying float from MySQL gives inaccurate results
<p>To show my problem I made two functions. The first asks Laravel for a PDO instance and queries the database. In the second I instanciate a new one myself.
The first one from Laravel gives inaccurate results. The second one gives the right results.</p>

<p>The table defenition would be something like this:</p>

<pre><code>CREATE TABLE IF NOT EXISTS `event` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `level` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
</code></pre>

<p>This is the first function:</p>

<pre><code>public function testpdo1() {
    $pdo = DB::connection('myconnection')-&gt;getPdo();        
    $query = "SELECT level FROM event LIMIT 4";
    $result = $pdo-&gt;query($query);
    foreach ($result as $row) {
        print $row[0] ."\n";
    }
}
</code></pre>

<p>Which gives these results:</p>

<pre><code>102.09999847412
97.900001525879
95.300003051758
94.099998474121
</code></pre>

<p>This is the second function:</p>

<pre><code>public function testpdo2() {
    $pdo = new PDO('dsn', 'user', 'pass');
    $query = "SELECT level FROM event LIMIT 4";
    $result = $pdo-&gt;query($query);
    foreach ($result as $row) {
        print $row[0] ."\n";
    }
}
</code></pre>

<p>Which gives these results:</p>

<pre><code>102.1
97.9
95.3
94.1
</code></pre>

<p>The last ones are the correct results.</p>

<p>Is there a way to make Laravel behave like in the second function?</p>

<p>I suppose the difference is in PDO settings. I tried setting PDO::ATTR_STRINGIFY_FETCHES but to no avail. I also tried that in the second function to see if I could make the second go wrong. But it didn't work.</p>

<p>Maybe I do something wrong with setting an attribute like that. With laravel (function 1) I tried setting it /config/database.php as an option in my connection:</p>

<pre><code>'connections' =&gt; [

    'myconnection' =&gt; [
        'driver'    =&gt; 'mysql',
        'host'      =&gt; env('DB_HOST', 'localhost'),
        'database'  =&gt; env('DB_DATABASE', 'forge'),
        'username'  =&gt; env('DB_USERNAME', 'forge'),
        'password'  =&gt; env('DB_PASSWORD', ''),
        'charset'   =&gt; 'utf8',
        'collation' =&gt; 'utf8_unicode_ci',
        'prefix'    =&gt; '',
        'strict'    =&gt; false,
        'options'   =&gt; array (
            PDO::ATTR_STRINGIFY_FETCHES =&gt; true,
        ),
    ],
....
</code></pre>

<p>With the second function I tried it with </p>

<pre><code>$pdo = new PDO('dsn', 'user', 'pass', [PDO::ATTR_STRINGIFY_FETCHES =&gt; true]);
</code></pre>

<p>and with</p>

<pre><code>$pdo-&gt;setAttribute ( PDO::ATTR_STRINGIFY_FETCHES , true );
</code></pre>

<p>after instanciation, with true an false values. It doesn't seem to make any difference...</p>

<p>This question comes close to my question: <a href="https://stackoverflow.com/questions/17048057/php-pdo-query-returns-inaccurate-value-for-float-fields">PHP PDO query returns inaccurate value for FLOAT fields</a>
. But since my second function gives the right answers it seems possible to get the accurate answer.</p>

<p>@Ragu Swaminathan suggests the second function gives rounded values. Just to be clear: those are the values that are in the database.</p>

<pre><code>mysql&gt; select level from event LIMIT 4;
+-------+
| level |
+-------+
| 102.1 |
|  97.9 |
|  95.3 |
|  94.1 |
+-------+
4 rows in set (0.00 sec)
</code></pre>

<p>When I use rounding in the query of function 1 the values become the same as the values from function 2. That is a possible work around but I hope for a better solution.</p>