Thursday, April 14, 2011

MySql及PHP 中文設定

mysql> status;
--------------
mysql Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (i486) using readline 5.2
Connection id: 226
Current database: taxi
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.0.51a-24+lenny4 (Debian)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: utf8
Client characterset: latin1
Conn. characterset: latin1

UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 1 day 41 min 6 sec
Threads: 1 Questions: 1068 Slow queries: 0 Opens: 665 Flush tables: 1 Open tables: 64 Queries per second avg: 0.012
--------------

原本粗體全部為latin1, 用alter database db_name collate utf8_unicode_ci;
Db Characterset變為utf8.

用set names uft8;則Client 及 Conn. Characterset 都變為utf8

寫的PHP網頁由DB提取中文字出現問號, 在mysql console 下亦以問號(同debian無裝中文有關).phpmyadmin下insert 同select 都可以正常顯示,create table 時已加character set = utf8, 尚在處理中...

試左一輪, 响mysql同php連線前用mysql_query("set names utf8;");則之後的query 的中文正確顯示不再出現問號

參考以下几個網頁

資料庫內,某個 table 內某個 column 的 collation 錯誤造成無法儲存中文
http://0rz.tw/6NWaF

MySQL中文編碼徹底研究 *有點舊,但中文原理講得詳細
http://tw.myblog.yahoo.com/waian-950604/article?mid=1631&prev=1636&next=1630

mysql插入中文出乱码
http://www.nowamagic.net/database/db_CharsetPrinciple.php

让MYSQL彻底支持中文(slack10.1版)
http://slack.linuxsir.org/main/?q=node/165

MySQL參考手冊 第10章:字符集支持
http://dev.mysql.com/doc/refman/5.1/zh/charset.html

9.1.4. Connection Character Sets and Collations
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

php 解决乱码的通用方法
http://blog.51yip.com/php/572.html

InnoDB 匯入時關閉 Foreign Key 檢查及支援UTF8 (在SQL滙入的文件中加入)
http://jax-work-archive.blogspot.com/2009/07/innodb-foreign-key-mysql.html
SET FOREIGN_KEY_CHECKS = 0; //0 turn off
SET NAMES 'UTF8';

mysqldump & mysql command for UTF-8 database
http://jax-work-archive.blogspot.com/2011/01/mysql-mysqldump-mysql-command-for-utf-8.html

utf8中文编码范围
詳細介紹UTF8中文及原理及例子
http://www.sphinxsearch.org/archives/40