PHP’s libmysql

Update(2006/6/28):這個文章的不是每一個解法都是有效。請看今天新刊出的MySQL 5.1 CJK編碼FAQ(MySQL5.0)。

Update(28 June, 2006):I learned that not all solutions in this post can solve MySQL-PHP CJK issues. Please refer to newly added MySQL 5.1 CJK FAQ session(MySQL 5.0) in MySQL Reference Manual.

根據文件裡的下面這句話,PHP的libmysql模組似乎只能和MySQL Server建立utf8編碼(英文)的連線。

Note: If you need charsets other than latin (default), you have to install external (not bundled) libmysql with compiled charset support.

那用utf8(utf-8,MySQL的命名錯誤)的PHP程式,像是這個blog到底是怎麼和MySQL建立連線的呢? 其實用utf8的連線存取資料庫還是可以使用;因為在Windows以外平台的MySQL換預設字碼要重新compile,所以大家將錯就錯用utf8而且也相安無事。只是當想要把資料整個匯出升級MySQL版本、設定編碼的時候就會發生像gslin’s Blog上次爆炸那樣的事情。另外的方法就是使用utf8編碼的資料庫,然後明確的在使用mysql_connect()的時候隨即聲明此連線的編碼格式(這兩行程式碼的Credit也是gslin的):

mysql_query("SET NAMES 'UTF8'");
mysql_query("SET CHARACTER SET UTF8");

這樣PHP就會乖乖的處理傳來的utf8文字;這和說明文件裡面寫的不一樣,不過可以運作。
問題來了。外面沒有哪個寫好的PHP程式會在連線時傳這兩行出去,就連WordPress也不會。所以我(學gslin大)必須把這兩行手動加進WordPress的程式碼。至於像Joomla那種複雜的程式呢? 回去用utf8存取資料吧。
喔,請不要以為我太閒做了這些個人研究來貼blog;我現在在用Joomla架設系學會網站(網址尚未啟用),所以@$#%$*!….(翻桌)。而我這個blog這台機器現在MySQL也怪怪的…唉。

The documentation of PHP’s libmysql module states that it cannot connect to MySQL server with charset other than utf8. Since a MySQL server that has non-utf8 as default charset require re-compile, people store their utf-8 content in a utf8 database. It’s fine, in fact, it will be fine until one day you done thing that gslin had do.
To access a utf8 database, one need following two lines every time one mysql_connect()(Credit goes to gslin):

mysql_query("SET NAMES 'UTF8'");
mysql_query("SET CHARACTER SET UTF8");

Here is the problem. Native WordPress code doesn’t contain these lines; nothing does actually. For me I edited the source code of WordPress, but I can’t do it on complicate code such as Joomla; I still have to store my data in a utf8 database, make it a wrong+wrong=right situation.
FYI: I did not make these research because I am bored; I have install a Joomla CMS for Student Association website, and *boom*.
orz.