問題描述
我在使用 PHP 時遇到了編碼問題.
I am having trouble with PHP regarding encoding.
我有一個 JavaScript/jQuery HTML5 頁面使用 $.post 與我的 PHP 腳本交互.但是,PHP 面臨著一個奇怪的問題,可能與編碼有關.
I have a JavaScript/jQuery HTML5 page interact with my PHP script using $.post. However, PHP is facing a weird problem, probably related to encoding.
當我寫
htmlentities("í")
我希望 PHP 輸出 í
.但是,它會輸出 í
一開始,我以為我在編碼上犯了一些錯誤,但是
I expect PHP to output í
. However, instead it outputs í
At the beginning, I thought that I was making some mistake with the encodings, however
htmlentities("í")=="í"?"Good":"Fail";
正在輸出失敗",其中
htmlentities("í")=="í"?"Good":"Fail";
但是 htmlentities($search, null, "utf-8")
按預期工作.
我想讓 PHP 與 MySQL 服務器通信,但它也有編碼問題,即使我使用 utf8_encode.我該怎么辦?
I want to have PHP communicate with a MySQL server, but it has encoding problems too, even if I use utf8_encode. What should I do?
在 SQL 命令上,寫
On the SQL command, writing
SELECT id,uid,type,value FROM users,profile
WHERE uid=id AND type='name' AND value='XXX';
其中 XXX 不包含 í 字符,按預期工作,但如果有任何 'í' 字符,則不會.
where XXX contains no í chars, works as expected, but it does not if there is any 'í' char.
SET NAMES 'utf8';
SET CHARACTER SET 'utf8';
SELECT id,uid,type,value FROM users,profile
WHERE uid=id AND type='name' AND value='XXX';
不僅對于í字符失敗,而且對于沒有任何特殊"字符的字符串也會失敗.從 SET NAMES 和 SET CHARACTER SET 中刪除 ' 字符似乎沒有任何改變.
Not only fails for í chars, but it ALSO fails for strings without any 'special' characters. Removing the ' chars from SET NAMES and SET CHARACTER SET doesn't seem to change anything.
我正在使用 PDO 連接到 MySQL 數據庫.
I am connecting to the MySQL database using PDO.
編輯 2:我使用的是 XAMPP for Linux 的 MySQL 5.1.30 版.
EDIT 2: I am using MySQL version 5.1.30 of XAMPP for Linux.
編輯 3:從 PhpMyAdmin 輸出運行 SHOW VARIABLES LIKE '%character%'
EDIT 3: Running SHOW VARIABLES LIKE '%character%'
from PhpMyAdmin outputs
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)輸出運行相同的查詢:
Running the same query from my PHP script(with print_r) outputs:
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<的編碼很重要/a> 匹配輸入,就像您在最后一個示例中所做的那樣,但在前三個示例中省略了.
It's very important to specify the encoding of htmlentities to match that of the input, as you did in your final example but omitted in the first three.
htmlentities($text,ENT_COMPAT,'utf-8');
關于與 MySQL 的通信,您需要確保連接排序規則和字符集與您正在傳輸的數據匹配.您可以在配置文件中進行設置,也可以在運行時使用以下查詢進行設置:
Regarding communications with MySQL, you need to make sure the connection collation and character set matches the data you are transmitting. You can either set this in the configuration file, or at runtime using the following queries:
SET NAMES utf8;
SET CHARACTER SET utf8;
確保表、數據庫和服務器字符集也匹配.有一種設置不能在運行時更改,那就是服務器的字符集.需要在配置文件中修改:
Make sure the table, database and server character sets match as well. There is one setting you can't change at run-time, and that's the server's character set. You need to modify it in the configuration file:
[mysqld]
character-set-server = utf8
default-character-set = utf8
skip-character-set-client-handshake
閱讀更多關于 MySQL 中的字符集和排序規則在手冊中一>.
Read more on characters sets and collations in MySQL in the manual.
這篇關于PHP/MySQL 有編碼問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!