# MySQL, Prepared Statements, and Fractional Seconds

> Section in “New and Extended Functionality › Date and Time” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/date-and-time/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:

``` php
$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.

---

Previous: [High-Resolution Monotonic Timer](https://php7explained.com/new-and-extended-functionality/date-and-time/high-resolution-monotonic-timer/index.html.md) | Next: [LDAP](https://php7explained.com/new-and-extended-functionality/ldap/index.html.md)
