PHP with PDO not return proper native value from database
I have tried many versions of PHP & Alpine docker images. Specially cause problem with float value not returning native value same as DB stored.
It's return float(11.1) instead of float(11.11)
Dockerfile
FROM php:7.3.13-fpm-alpine3.10
RUN apk update --no-cache \
&& apk add --no-cache $PHPIZE_DEPS \
&& docker-php-ext-install pdo pdo_mysql
Docker up & Run php test file not returning proper native value for float datatype in Mysql
Set PDO::ATTR_EMULATE_PREPARES = false to get native db value.
test.php
<?php
// DB configuration
$host = 'localhost';
$user = 'test';
$dbPass = 'test';
$dbname = 'test';
$dsn = 'mysql:host='. $host .';dbname='. $dbname;
// PDO connection and settings
$pdo = new PDO($dsn, $user, $dbPass);
// It's required otherwise return value in string instead of float
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// Here amount is float(5,2) & Value = 11.11
$sql = 'SELECT id, amount FROM account WHERE id=1236';
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo '<pre>';
var_dump($row['amount']); // It's return float(11.1) instead of float(11.11)
echo "\n";
echo '</pre>';
exit;