开发者

How do I insert UCS-2 data with PHP PDO into MySQL?

开发者 https://www.devze.com 2023-03-31 03:53 出处:网络
The manual clearly states \" ucs2 cannot be used as a client character set, which means that it does not work for SET NAMES or SET CHARACTER SET\". So how can I insert, for example, the codepoi开发者_

The manual clearly states " ucs2 cannot be used as a client character set, which means that it does not work for SET NAMES or SET CHARACTER SET". So how can I insert, for example, the codepoi开发者_开发问答nt U+2193? I am using PHP 5.3 + PDO.


If you want to use Unicode for communicating with a MySQL server, your only option is to use UTF-8.

If you're working with UCS-2 or UTF-16 strings in PHP now, you'll have to convert them to UTF-8 before trying to store them. Also note that MySQL will give you back UTF-8 if that's what you set your client character set to, so you'll need to convert query results as well if you're committed to working with UCS-2 on the PHP side. (If you're in a position to make bigger changes, you'd likely be better off simply using UTF-8 everywhere than doing all this extra conversion.)

As for storing the codepoint U+2193, no worries: UTF-8 can represent every Unicode codepoint (in this specific case, it'd be 0xE2 0x86 0x93).

Technically, this is fudging a little, since MySQL's utf8 and ucs2 character sets only cover a subset of Unicode called the Basic Multilingual Plane (BMP). The world of Unicode charsets is expanded in MySQL 5.5 to move beyond the BMP, but you still can't use ucs2, the new utf16 or utf32 charsets as client charsets, leaving you still stuck with UTF-8.


For posterity, CREATE TABLE test (encoding varchar(255) CHARACTER SET ucs2); and then INSERT INTO test VALUES (1, CHAR(0x2193));. If I then run a SELECT * FROM test I see a down arrow.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号