php和mysql支持utf8

老生常谈了,网上很多文,我再多写一篇!

1. my.cnf里加上:
[client]
default-character-set=utf8

[mysqld]
character_set_server=utf8

重启mysqld,连上去看看:

mysql> SHOW VARIABLES LIKE '%character%';SHOW VARIABLES LIKE 'collation%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
 
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

2. php里也要改,写个函数:

 
function connect_db($dbname)
{
 global $mysql_addr, $mysql_user, $mysql_pass;
 $dbcon = mysql_connect($mysql_addr, $mysql_user, $mysql_pass);
 mysql_query("SET NAMES 'utf8'; SET CHARACTER_SET_CLIENT=utf8; "
."SET CHARACTER_SET_RESULTS=utf8; ", $dbcon);
 if ($dbname)
 {
  mysql_select_db($dbname, $dbcon);
 }
 return $dbcon;
}

3. 数据库可以改一下:

alter database mydb CHARACTER SET  ‘utf8’ ;      

alter table tablename CHARACTER SET ‘utf8’;

不过这两句不会改内容,如果内容原来不是utf8的字节,就要导出导入了.

此条目发表在开发分类目录。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

*