Skip to content

PDO MySQL returns php value type "string" for column type "YEAR" #1631

@dortort

Description

@dortort

Description

The following code:

<?php
/**
 * Given:
 * CREATE TABLE `t` (
 *   `y` year(4) DEFAULT NULL,
 *   `m` tinyint(3) DEFAULT NULL
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8
 * INSERT INTO t VALUES (2001, 11);
 */
$pdo = new PDO($dsn, null, null, [ PDO::ATTR_EMULATE_PREPARES => false ]);
var_dump($pdo->query("SELECT * FROM t")->fetchAll(PDO::FETCH_ASSOC));

Resulted in this output:

array(1) {
  [0]=>
  array(2) {
    ["y"]=>
    string(4) "2001"
    ["m"]=>
    int(11)
  }
}

But I expected this output instead:

array(1) {
  [0]=>
  array(2) {
    ["y"]=>
    int(2001)
    ["m"]=>
    int(11)
  }
}

This breaking change occurs between PHP v8.0 & v8.1.

PHP Version

8.1.6

Operating System

Alpine 10.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions