PHP / MySQL具有编码问题 [英] PHP/MySQL with encoding problems

查看:126
本文介绍了PHP / MySQL具有编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个JavaScript / jQuery HTML5页面使用$ .post与我的PHP脚本进行交互。
但是,PHP面临着一个奇怪的问题,可能与编码有关。



当我写

  htmlentities(í)

我希望PHP输出&安培; iacute; 。但是,相反,它输出& Atilde;& shy;
最初,我以为我在编码方面犯了一些错误,但是

  htmlentities(í)==& iacute?好:失败 

正在输出失败,其中

 ヶ辆( I)== &安培; Atilde;&安培;害羞; 良好: 失败;? 

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不包含í字符,按预期工作,但如果有í '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中删除'chars似乎没有任何变化。



我使用PDO连接到MySQL数据库。 p>

编辑2:我使用MySQL版本5.1.30的XAMPP for Linux。



编辑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] =&


[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 匹配输入,就像你在最后一个例子中所做的那样,但前三个省略。

 ヶ辆($文本,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中的字符集和排序规则在手册


I am having trouble with PHP regarding encoding.

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.

When I write

htmlentities("í")

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";

is outputing "Fail", where

htmlentities("í")=="í"?"Good":"Fail";

But htmlentities($search, null, "utf-8") works as expected.

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?

EDIT: On the SQL command, writing

SELECT id,uid,type,value FROM users,profile
WHERE uid=id AND type='name' AND value='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';

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.

I am connecting to the MySQL database using PDO.

EDIT 2: I am using MySQL version 5.1.30 of XAMPP for Linux.

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/

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/
        )

)

Running

SET NAMES 'utf8';
SET CHARACTER SET 'utf8';
SHOW VARIABLES LIKE '%character%'

outputs an empty array.

解决方案

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');

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

Read more on characters sets and collations in MySQL in the manual.

这篇关于PHP / MySQL具有编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆