すみません、初めてマトモな質問をします。よろしくお願いします。
MySQL、PostgreSQLでマイクロ秒のカラムを作り、INSERTするのは出来るのですが、SELECTしてfetchすると秒単位に切り詰められてしまうことに気づきました。
php.netのPDOのマニュアルをざっと見てみたのですが、該当の記述が見当たりませんでした。
PDOでマイクロ秒を取得することは出来ないんでしょうか?
PHP
1<?php 2 $dsn = "mysql:dbname=test_db;port=3306;host=localhost"; 3 $hdl = new PDO($dsn, "test_user", "testtest"); 4 5 if(!$hdl){ echo "could not connect"; exit; } 6 7 $hdl->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 8 $hdl->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 9 10 $qry = "DROP TABLE `hoges`"; 11 $stmt = $hdl->prepare($qry . ";"); 12 $result = $stmt->execute(); 13 if(!$result){ echo "drop table failed"; exit; } 14 15 $qry = <<< EOL 16CREATE TABLE `hoges` ( 17`idx` INT COLLATE utf8mb4_general_ci NOT NULL DEFAULT 0 COMMENT 'ID', 18`add_at` TIMESTAMP(6) COLLATE utf8mb4_general_ci NOT NULL COMMENT 'add_time', 19PRIMARY KEY `p_hoges`(`idx`) 20) ENGINE=InnoDB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci COMMENT='hogeテーブル'; 21EOL; 22 23 $stmt = $hdl->prepare($qry . ";"); 24 $result = $stmt->execute(); 25 if(!$result){ echo "create table failed"; exit; } 26 27 $qry = "INSERT INTO `hoges`(idx, add_at) VALUES (1, '2016-03-22 19:38.01.123456')"; 28 $stmt = $hdl->prepare($qry . ";"); 29 $result = $stmt->execute(); 30 if(!$result){ echo "insert failed"; exit; } 31 32 $qry = "SELECT * FROM `hoges` WHERE idx = 1"; 33 $stmt = $hdl->prepare($qry . ";"); 34 $result = $stmt->execute(); 35 if(!$result){ echo "select failed"; exit; } 36 37 $result = $stmt->fetchAll(PDO::FETCH_OBJ); 38 print_r($result);
出力結果
Array ( [0] => stdClass Object ( [idx] => 1 [add_at] => 2016-03-22 19:38:01 ) )
追記:
ふと検索したstackoverflowを見ると、
http://stackoverflow.com/questions/22026570/php-how-to-return-datetime6-from-mysql
かなり古い話ですが、PDOのバグだというような内容が…
まだ治っていないということでしょうか…

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/03/22 11:37
退会済みユーザー
2016/03/22 11:39
退会済みユーザー
2016/03/22 11:55
2016/03/22 12:52
2016/03/22 12:52
退会済みユーザー
2016/03/22 13:15
2016/03/22 16:31 編集
退会済みユーザー
2016/03/23 11:04
2016/03/23 13:02
2016/03/24 20:13 編集