PHP应用程序中的MySQL字符编码(öä)

时间:2022-05-21 21:37:35

Hello I have a character encoding problem in my application and thought to ask for some help, because I couldn't solve the problem even thought I was given some guidance so here goes: My Ä and Ö characters are shown in the browser as: �

您好我的应用程序中有一个字符编码问题,并且想要求一些帮助,因为我无法解决问题,即使我得到了一些指导,所以这里说:我的Ä和Ö字符在浏览器中显示为:

I will also post all what I have done so far trying to solve the problem:

我还将发布迄今为止我尝试解决问题的所有内容:

1) Database: I have tried changing the collation of my tables, here are some info what SHOW TABLE STATUS gives for one of my tables:

1)数据库:我已经尝试更改表的排序规则,这里有一些SHOW TABLE STATUS为我的表之一提供的信息:

Name = test_groups
Engine = InnoDB
Version = 10
Row_format = Compact
Collation = utf8_swedish_ci

Name = test_groups Engine = InnoDB Version = 10 Row_format = Compact Collat​​ion = utf8_swedish_ci

Database character variables gives:

数据库字符变量给出:

| character_set_client = utf8
| character_set_connection = utf8
| character_set_database = latin1 (I Wonder is this the cause?)
| character_set_filesystem = binary
| character_set_results = utf8
| character_set_server = utf8
| character_set_system = utf8

| character_set_client = utf8 | character_set_connection = utf8 | character_set_database = latin1(我不知道这是原因吗?)| character_set_filesystem = binary | character_set_results = utf8 | character_set_server = utf8 | character_set_system = utf8

2) In apache httpd.conf I have:

2)在apache httpd.conf中我有:

AddDefaultCharset UTF-8

AddDefaultCharset UTF-8

3) In my Zend-application application.ini:

3)在我的Zend应用程序application.ini中:

resources.view.encoding = "UTF-8"

resources.view.encoding =“UTF-8”

4) In my firefox 14.0.1 browser

4)在我的firefox 14.0.1浏览器中

edit->preferences->content->advanced->Default character encoding = Unicode (UTF-8)

edit-> preferences-> content-> advanced->默认字符编码= Unicode(UTF-8)

5) In my php code meta-tag:

5)在我的PHP代码元标记中:

<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">

Now here's also few other interesting things: When I look at my page and change from firefox

现在这里还有其他一些有趣的事情:当我查看我的页面并从firefox更改时

View->Character encoding->Western (ISO-8859-1)

查看 - >字符编码 - >西方(ISO-8859-1)

, the �-characters which came from the MySQL database turn out ok to öä-characters, but the öä-characters that come from my php-code turn into ät-characters.

来自MySQL数据库的 字符对于öä字符来说是好的,但是来自我的php代码的ä字符变成了t字符。

Another thing when I check the encoding of the data coming from my MySQL-database with

当我检查来自我的MySQL数据库的数据的编码时,另一件事

mb_detect_encoding($DATA_FROM_MYSQL_DATABASE)

mb_detect_encoding($ DATA_FROM_MYSQL_DATABASE)

it outputs UTF-8!! Then lastly if I do in the code:

它输出UTF-8 !!最后,如果我在代码中做:

utf8_encode($DATA_FROM_MYSQL_DATABASE)

函数utf8_encode($ DATA_FROM_MYSQL_DATABASE)

and output the result the problem disappears that is �-characters -> öä-characters. So what's going on here x) All help appreciated

并输出结果问题消失,即 字符 - >öä字符。那么这里发生了什么x)所有的帮助表示赞赏

2 个解决方案

#1


2  

Are you sending SET NAMES utf8 in your PHP as the first query to MySQL ? That could be the cause if not.

您是否在PHP中发送SET NAMES utf8作为MySQL的第一个查询?如果没有,这可能是原因。

SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES 'cp1251' tells the server, “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)

SET NAMES表示客户端将用于将SQL语句发送到服务器的字符集。因此,SET NAMES'cp1251'告诉服务器,“来自此客户端的未来传入消息是字符集cp1251。”它还指定了服务器用于将结果发送回客户端的字符集。 (例如,如果使用SELECT语句,它指示要用于列值的字符集。)

SET NAMES utf8 in MySQL? has more detail about how and why.

在MySQL中设置名称utf8?有更多关于如何以及为什么的细节。

#2


1  

Troubleshoot:

故障排除:

Check your database (with PHPMyAdmin, for instance). Are the characters correctly stored? Or does it seem gibberish?

检查数据库(例如,使用PHPMyAdmin)。字符是否正确存储?还是看起来像胡言乱语?

If the characters in the database are ok, then the problem happens when retrieving. If they are stored incorrectly (as I would guess they are), then the problem is in the "storing".

如果数据库中的字符正常,则检索时会出现问题。如果它们存储不正确(正如我猜的那样),则问题在于“存储”。

  1. Check your source code file and verify if they are encoded in UTF-8.
  2. 检查源代码文件并验证它们是否以UTF-8编码。
  3. Force mysql connection to use UTF8 (mysqli::set_charset('utf8') or mysql_set_charset('utf8') or PDO: Add charset to the connection string (charset=utf8) )
  4. 强制mysql连接使用UTF8(mysqli :: set_charset('utf8')或mysql_set_charset('utf8')或PDO:将charset添加到连接字符串(charset = utf8))

#1


2  

Are you sending SET NAMES utf8 in your PHP as the first query to MySQL ? That could be the cause if not.

您是否在PHP中发送SET NAMES utf8作为MySQL的第一个查询?如果没有,这可能是原因。

SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES 'cp1251' tells the server, “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)

SET NAMES表示客户端将用于将SQL语句发送到服务器的字符集。因此,SET NAMES'cp1251'告诉服务器,“来自此客户端的未来传入消息是字符集cp1251。”它还指定了服务器用于将结果发送回客户端的字符集。 (例如,如果使用SELECT语句,它指示要用于列值的字符集。)

SET NAMES utf8 in MySQL? has more detail about how and why.

在MySQL中设置名称utf8?有更多关于如何以及为什么的细节。

#2


1  

Troubleshoot:

故障排除:

Check your database (with PHPMyAdmin, for instance). Are the characters correctly stored? Or does it seem gibberish?

检查数据库(例如,使用PHPMyAdmin)。字符是否正确存储?还是看起来像胡言乱语?

If the characters in the database are ok, then the problem happens when retrieving. If they are stored incorrectly (as I would guess they are), then the problem is in the "storing".

如果数据库中的字符正常,则检索时会出现问题。如果它们存储不正确(正如我猜的那样),则问题在于“存储”。

  1. Check your source code file and verify if they are encoded in UTF-8.
  2. 检查源代码文件并验证它们是否以UTF-8编码。
  3. Force mysql connection to use UTF8 (mysqli::set_charset('utf8') or mysql_set_charset('utf8') or PDO: Add charset to the connection string (charset=utf8) )
  4. 强制mysql连接使用UTF8(mysqli :: set_charset('utf8')或mysql_set_charset('utf8')或PDO:将charset添加到连接字符串(charset = utf8))