MySQL, Prepared Statements, and Fractional Seconds

MySQL has support for fractional seconds in time values. A column with type TIMESTAMP(6), for instance, contains a UNIX timestamp with microsecond resolution:

> SELECT t FROM example;
+----------------------------+
| t                          |
+----------------------------+
| 2020-07-02 08:16:02.752865 |
+----------------------------+

Prior to PHP 7.3, fractional seconds of a time value were truncated when a column of that type was queried using a prepared statement:

$connection = new MySQLi('localhost', 'test', 'password', 'test');

$statement = $connection->prepare('SELECT t FROM example;');
$statement->execute();

$result = $statement->get_result();
$row    = $result->fetch_assoc();

var_dump($row['t']);

Executing the code shown above with PHP 7.2 and earlier prints the output shown below:

string(19) "2020-07-02 08:16:02"

The fractional seconds part, .752865, is truncated. Executing the same code with PHP 7.3 and newer prints the output shown below:

string(26) "2020-07-02 08:16:02.752865"

The example shown above uses the mysqli extension to communicate with the MySQL server. The same improvement has been implemented for prepared statements that are executed using the pdo_mysql extension.