From: Operating system: Linux PHP version: Irrelevant Package: MySQLi related Bug Type: Feature/Change Request Bug description:Add "default character set" option for mysql connection
Description: ------------ I'm voting for http://bugs.php.net/bug.php?id=44118 Here is my explanation: This is actually not implemented feature of mysql protocol (character set handshake), related to both mysql and mysqli PHP extensions causing buggy behavior. While connecting, several packets exchanges between server and client. After succesful TCP handshake, server sent "Server Greeting" message (packet) where server's default character set is specified (for example "Charset: utf8 COLLATE utf8_general_ci" - 0x21). Then "Login Request" message follows from client's side specifying character set which will be used for connection. Here is problem - PHP's both mysql and mysqli extension send always "Charset: latin1 COLLATE latin1_swedish_ci" - 0x08. But PHP don't look at query responses in way they are encoded in latin1. But mysql database must re-encode data from charset used for table field to latin1. There is actually 2 different solutions for this problem (mysqli [mysql]): 1. solution: executing mysqli::set_charset [mysql_set_charset] right after mysqli::__construct [mysql_ connect]. But this causes redundant query sent to server (for example: SET NAMES "utf8"); 2. solution: configure mysql server with option "character-set-client-handshake = false" which makes MySQL behave like MySQL 4.0 and ignore character set sent by client. I think there should be MySQL/MySQLi Configuration Option like: mysqli.default_character_set = utf8 mysql.default_character_set = utf8 This will support mysql's character set handshake without executing redundant queries. This option is also possible in mysql cli interface as "--default-character-set=utf8" Here is Login Request message caught by wireshark for code: <?php mysql_connect("127.0.0.1", "root", "test"); ?> (character set in "[]" brackets) 0000 3a 00 00 01 85 a2 02 00 00 00 00 40[08]00 00 00 :..........@.... 0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0020 00 00 00 00 72 6f 6f 74 00 14 0c 79 bf 1a bd d0 ....root...y.... 0030 36 0a 21 fc 6d e5 f0 42 27 89 9f 32 e5 95 6.!.m..B'..2.. Here is Login Request message caught by wireshark for code (cli): $ mysql -u root -p -h 127.0.0.1 --default-character-set=utf8 (character set in "[]" brackets) 0000 3a 00 00 01 85 a6 03 00 00 00 00 01[21]00 00 00 :...........!... 0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0020 00 00 00 00 72 6f 6f 74 00 14 97 58 9b 0a 65 b8 ....root...X..e. 0030 0c 67 dc 8a 82 28 51 a5 1f 1b 08 28 c5 c2 .g...(Q....(.. Test script: --------------- <?php mysql_connect("127.0.0.1", "root", "test"); ?> cli: mysql -u root -p -h 127.0.0.1 --default-character-set=utf8 -- Edit bug report at http://bugs.php.net/bug.php?id=54086&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=54086&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=54086&r=trysnapshot53 Try a snapshot (trunk): http://bugs.php.net/fix.php?id=54086&r=trysnapshottrunk Fixed in SVN: http://bugs.php.net/fix.php?id=54086&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=54086&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=54086&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=54086&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=54086&r=needscript Try newer version: http://bugs.php.net/fix.php?id=54086&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=54086&r=support Expected behavior: http://bugs.php.net/fix.php?id=54086&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=54086&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=54086&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=54086&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54086&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=54086&r=dst IIS Stability: http://bugs.php.net/fix.php?id=54086&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=54086&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=54086&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=54086&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=54086&r=mysqlcfg