Precision loss when inserting double into MySQL via PDO

I came across this very annoying behavior, and I want to know that I am doing something wrong, or if it is intentional (and if so, why).

Whenever I have a variable in php (5.3) that is of type double, and I want to insert it into the database (MYSQL 5.0) in a field of type double, the value is always rounded to 6 digits when I use PDO. So the code below:

$stmt = $pdo->prepare("UPDATE someTable SET someDouble = :somePHPDouble;"); $number = 0.11124379542256; $stmt->bindValue(':somePHPDouble', $number); $stmt->execute(); 

Results at 0.111244 inserted in db. But when I passed the variable to the string (!) In the binding expression, for example:

 $stmt->bindValue(':somePHPDouble', (string)$number); 

he inserts it correctly as 0.11124379542256.

What's going on here? I am ignorant. MySQL datatype someDouble is really double, and when pasting it through mysql console it just works. And the variable in php is really double, so it seems to me that something is wrong inside the PDO.

Thanks in advance, -CodePoet.

+4
source share
1 answer

This is not intentional, and you are not doing something wrong. This seems to be a PHP bug.

According to this bug report , it was fixed for PHP 5.2.11, but only recently for branch 5.3, so you might want to check your exact version of PHP for the ones listed there.

+6
source

All Articles