简体   繁体   English

MySQL的PHP​​ UTF-8

[英]mysql php UTF-8

There's a strange problem here: 这里有一个奇怪的问题:

i have to write data from a CSV file into a mysql DB (default charset UTF-8) 我必须将数据从CSV文件写入mysql数据库(默认字符集UTF-8)

when i read the CSV and print the query string on terminal (for debug purpose )the output is correct: 当我读取CSV并在终端上打印查询字符串(用于调试目的)时,输出是正确的:

insert into orari (pv_id, day) values(5492, 'giovedì pomeriggio')

but when i run a select query with mysql client the result is weird: 但是当我使用mysql客户端运行选择查询时,结果很奇怪:

*************************** 273. row ***************************
             id: 28856
             day: giovedì pomeriggio
             pv_id: 5760

Same thing if i run the query on phpmyadmin. 如果我在phpmyadmin上运行查询,同样的事情。

The HTML output is correct, the charset is set to UTF-8 and the characters like àòù are correctly visualized. HTML输出正确,字符集设置为UTF-8,像àòù这样的字符可以正确显示。

* Edit * This is my mysql charset and collation settings: *编辑*这是我的mysql字符集和整理设置:

SHOW VARIABLES LIKE 'c%';

| 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                                                  |
| collation_connection     | utf8_general_ci                                       |
| collation_database       | utf8_general_ci                                       |
| collation_server         | utf8_general_ci                                       |

Each MySQL client should specify the charset they want when they handshake with the server. 每个MySQL客户端应在与服务器握手时指定所需的字符集。 Unless overidden the default is latin1 . 除非覆盖,否则默认值为latin1

You can issue the following query on every connection to work around this: 您可以在每个连接上发出以下查询以解决此问题:

SET NAMES 'utf8';

Or you can set it in the MySQL charset configuration as described in the MySQL manual. 或者您可以按照MySQL手册中的描述在MySQL字符集配置中进行设置。

You say that the HTML and the input is correct, but only mysql and phpmyadmin are incorrect. 你说HTML和输入是正确的,但只有mysqlphpmyadmin是不正确的。 You need to set the proper connection charset for these as well, since they default to latin1 which means "8-bit clean, one byte per char". 你需要为这些设置正确的连接字符集,因为它们默认为latin1 ,这意味着“8位清理,每个字符一个字节”。

(This shouldn't affect your problem, but you should make sure your table or column specify utf-8 storage.) (这不应该影响您的问题,但您应该确保您的表或列指定utf-8存储。)

You'll notice your garbage output is two bytes, which means that what is stored in the DB is definitely UTF8. 您会注意到您的垃圾输出是两个字节,这意味着存储在数据库中的内容肯定是UTF8。 Your data is being misinterpreted on output. 您的数据在输出时被误解释。

For the mysql terminal program, start it with the --default-character-set parameter. 对于mysql终端程序,使用--default-character-set参数启动它。 Make sure it matches your terminal's character set. 确保它与终端的字符集匹配。

I don't know how this is set with phpMyAdmin, but there should be a setting for connection character set as well. 我不知道如何使用phpMyAdmin设置它,但是也应该有连接字符集的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM