我在编写PHP时遇到麻烦.
我有一个JavaScript/jQuery HTML5页面使用$ .post与我的PHP脚本交互.但是,PHP面临着一个奇怪的问题,可能与编码有关.
当我写作
htmlentities("í")
我希望PHP输出í
.然而,相反它输出Ã
在开始时,我认为我在编码时犯了一些错误
htmlentities("í")=="í"?"Good":"Fail";
正在输出"失败",其中
htmlentities("í")=="Ã"?"Good":"Fail";
但htmlentities($search, null, "utf-8")
按预期工作.
我想让PHP与MySQL服务器通信,但它也有编码问题,即使我使用utf8_encode.我该怎么办?
编辑:在SQL命令,写
SELECT id,uid,type,value FROM users,profile WHERE uid=id AND type='name' AND value='XXX';
其中XXX不包含任何字符,按预期工作,但如果有任何'í'字符则不然.
SET NAMES 'utf8'; SET CHARACTER SET 'utf8'; SELECT id,uid,type,value FROM users,profile WHERE uid=id AND type='name' AND value='XXX';
不仅没有íchars,但它也没有任何"特殊"字符的字符串失败.从SET NAMES和SET CHARACTER SET中删除'字符似乎没有任何改变.
我使用PDO连接到MySQL数据库.
编辑2:我正在使用XAMPP for Linux的MySQL版本5.1.30.
编辑3:SHOW VARIABLES LIKE '%character%'
从PhpMyAdmin输出运行
character_set_client utf8 character_set_connection utf8 character_set_database latin1 character_set_filesystem binary character_set_results utf8 character_set_server latin1 character_set_system utf8 character_sets_dir /opt/lampp/share/mysql/charsets/
从我的PHP脚本(带有print_r)运行相同的查询输出:
Array ( [0] => Array ( [Variable_name] => character_set_client [0] => character_set_client [Value] => latin1 [1] => latin1 ) [1] => Array ( [Variable_name] => character_set_connection [0] => character_set_connection [Value] => latin1 [1] => latin1 ) [2] => Array ( [Variable_name] => character_set_database [0] => character_set_database [Value] => latin1 [1] => latin1 ) [3] => Array ( [Variable_name] => character_set_filesystem [0] => character_set_filesystem [Value] => binary [1] => binary ) [4] => Array ( [Variable_name] => character_set_results [0] => character_set_results [Value] => latin1 [1] => latin1 ) [5] => Array ( [Variable_name] => character_set_server [0] => character_set_server [Value] => latin1 [1] => latin1 ) [6] => Array ( [Variable_name] => character_set_system [0] => character_set_system [Value] => utf8 [1] => utf8 ) [7] => Array ( [Variable_name] => character_sets_dir [0] => character_sets_dir [Value] => /opt/lampp/share/mysql/charsets/ [1] => /opt/lampp/share/mysql/charsets/ ) )
运行
SET NAMES 'utf8'; SET CHARACTER SET 'utf8'; SHOW VARIABLES LIKE '%character%'
输出一个空数组.
指定htmlentities的编码以匹配输入的编码非常重要,正如您在最后一个示例中所做的那样,但在前三个中省略了.
htmlentities($text,ENT_COMPAT,'utf-8');
关于与MySQL的通信,您需要确保连接排序规则和字符集与您正在传输的数据匹配.您可以在配置文件中设置它,也可以在运行时使用以下查询设置:
SET NAMES utf8; SET CHARACTER SET utf8;
确保表,数据库和服务器字符集也匹配.有一个设置在运行时无法更改,这是服务器的字符集.您需要在配置文件中修改它:
[mysqld] character-set-server = utf8 default-character-set = utf8 skip-character-set-client-handshake
阅读手册中有关MySQL 中字符集和排序规则的更多信息.
后期复兴.但为了进一步参考,这里有一些额外的提示:
使用mysql_set_charset而不是SET xxx
确保使用UTF-8编码保存文件(这经常被忽略)
设置标题:
如果您的Apache服务器配置包含具有不同编码的AddDefaultCharset指令,请向主机管理员大喊大叫.