|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75736 PDO's constructor parameter for DSN prevents setting collation
Submitted: 2017-12-26 18:37 UTC Modified: -
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: eagle at compu-lex dot de Assigned:
Status: Open Package: PDO MySQL
PHP Version: 5.6.32 OS: Debian 8 (Jessie)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: eagle at compu-lex dot de
New email:
PHP Version: OS:


 [2017-12-26 18:37 UTC] eagle at compu-lex dot de
PHP's MySQL PDO driver can't set collations without an init command, although the "charset=" parameter was introduced in PHP 5.3.6 to enable specifying the character set without the need for an init command. (See

See Anthony Rutledge's answer over at for further details regarding this issue.

Test script:
$db = new PDO('mysql:host=HostnameOrIpHere;dbname=DbNameHere;charset=utf8',
              array(PDO::ATTR_EMULATE_PREPARES => false,
                    PDO::ATTR_ERRMODE          => PDO::ERRMODE_EXCEPTION));
echo $db->query("SELECT COLLATION('foo')")->fetch(PDO::FETCH_NUM)[0];

Expected result:
PDO's MySQL driver should allow for setting the collation explicitly, i. e. via proper value for the "charset" parameter, e. g. changing test script's 2nd line to

$db = new PDO('mysql:host=HostnameOrIpHere;dbname=DbNameHere;charset=utf8_unicode_ci',

or by adding another parameter, e. g. "collation", for this should result in this output: utf8_general_ci

But since MySQL always uses "charset_collation" (optionally combined with "_ci" for case-insensitive comparison), I suggest only modifying which would be backwards compatible: utf8 -> works as before, utf8_unicode[_ci] -> utf8 for charset and unicode[_ci] for collation, if present.

Actual result:
Test script's expected output, utf8_general_ci

Workaround: using PDO::MYSQL_ATTR_INIT_COMMAND with a value of "SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'" (including quotes) as a third array element in the test script above, results in utf8_unicode_ci - but this should be considered as a temporary workaround as PHP introduced the "charset=" parameter to eliminate the need for setting it via init command.


Add a Patch

Pull Requests

Add a Pull Request

PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Jul 19 22:01:26 2019 UTC