Bug #75736 [NEW]: PDO's constructor parameter for DSN prevents setting collation

From: Date: Tue, 26 Dec 2017 18:37:53 +0000
Subject: Bug #75736 [NEW]: PDO's constructor parameter for DSN prevents setting collation
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213289@lists.php.net to get a copy of this message
From:             eagle at compu-lex dot de
Operating system: Debian 8 (Jessie)
PHP version:      5.6.32
Package:          PDO MySQL
Bug Type:         Bug
Bug description:PDO's constructor parameter for DSN prevents setting collation

Description:
------------
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
https://secure.php.net/manual/en/ref.pdo-mysql.connection.php)

See Anthony Rutledge's answer over at
https://stackoverflow.com/questions/25807202/how-to-specify-collation-with-pdo-without-set-names/32553447#32553447
for further details regarding this issue.

Test script:
---------------
<?php
$db = new
PDO('mysql:host=HostnameOrIpHere;dbname=DbNameHere;charset=utf8',
              'DbUserHere',
              'DbPasswordHere',
              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.

-- 
Edit bug report at https://bugs.php.net/bug.php?id=75736&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=75736&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=75736&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=75736&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=75736&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=75736&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=75736&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=75736&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=75736&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=75736&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=75736&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=75736&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=75736&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=75736&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75736&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=75736&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=75736&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=75736&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75736&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=75736&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=75736&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#213289) next »