Skip to content

Instantly share code, notes, and snippets.

@youkidearitai
Last active July 6, 2023 13:23
Show Gist options
  • Save youkidearitai/8f3a8046fbf0e26cc8872b2c0e791086 to your computer and use it in GitHub Desktop.
Save youkidearitai/8f3a8046fbf0e26cc8872b2c0e791086 to your computer and use it in GitHub Desktop.
<?php
$pdo = new PDO("mysql:host=127.0.0.1;dbname=ore", "root", "password");
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
$results = $pdo->query("SELECT * FROM test");
foreach ($results as $result) {
var_dump($result);
}
@youkidearitai
Copy link
Author

youkidearitai commented Jul 6, 2023

MySQL上 (on MySQL)

mysql> select * from test;
+-----------+------------+-------------+
| float_col | double_col | decimal_col |
+-----------+------------+-------------+
|      2.60 |       3.60 |        4.60 |
|      2.61 |       3.61 |        4.60 |
+-----------+------------+-------------+
2 rows in set (0.00 sec)

mysql>

実行結果 (result of execute)

array(6) {
  ["float_col"]=>
  string(3) "2.6"
  [0]=>
  string(3) "2.6"
  ["double_col"]=>
  string(3) "3.6"
  [1]=>
  string(3) "3.6"
  ["decimal_col"]=>
  string(4) "4.60"
  [2]=>
  string(4) "4.60"
}

@youkidearitai
Copy link
Author

このことから、 PDO::ATTR_EMULATE_PREPARES の影響でPHP 8.0でもPHP 8.1と同じ結果となった
trueもしくはコメントアウトなら 3.60 などとなる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment