SET整理和字符UTF8 - Mysql PDO

时间:2023-01-06 11:44:32

I would like to know the following: When you type in mysql the following commands:

我想知道以下内容:当您在mysql中键入以下命令时:

SHOW VARIABLES LIKE 'collation%';
SHOW VARIABLES LIKE 'character_set%';

It returns collation and character, so I configured to let them in UTF8 in this situation.

它返回校对和字符,所以我配置为在这种情况下让它们以UTF8。

array (size=3)
  0 => 
    array (size=2)
      'Variable_name' => string 'collation_connection' (length=20)
      'Value' => string 'latin1_swedish_ci' (length=17)
  1 => 
    array (size=2)
      'Variable_name' => string 'collation_database' (length=18)
      'Value' => string 'utf8_general_ci' (length=15)
  2 => 
    array (size=2)
      'Variable_name' => string 'collation_server' (length=16)
      'Value' => string 'latin1_swedish_ci' (length=17)
array (size=8)
  0 => 
    array (size=2)
      'Variable_name' => string 'character_set_client' (length=20)
      'Value' => string 'latin1' (length=6)
  1 => 
    array (size=2)
      'Variable_name' => string 'character_set_connection' (length=24)
      'Value' => string 'latin1' (length=6)
  2 => 
    array (size=2)
      'Variable_name' => string 'character_set_database' (length=22)
      'Value' => string 'utf8' (length=4)
  3 => 
    array (size=2)
      'Variable_name' => string 'character_set_filesystem' (length=24)
      'Value' => string 'binary' (length=6)
  4 => 
    array (size=2)
      'Variable_name' => string 'character_set_results' (length=21)
      'Value' => string 'latin1' (length=6)
  5 => 
    array (size=2)
      'Variable_name' => string 'character_set_server' (length=20)
      'Value' => string 'latin1' (length=6)
  6 => 
    array (size=2)
      'Variable_name' => string 'character_set_system' (length=20)
      'Value' => string 'utf8' (length=4)
  7 => 
    array (size=2)
      'Variable_name' => string 'character_sets_dir' (length=18)
      'Value' => string 'c:\wamp\bin\mysql\mysql5.6.12\share\charsets\' (length=45)

How do I use PDO like this:

我如何使用这样的PDO:

, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET UTF8"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET character_set_client=utf8"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET character_set_connection = utf8"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET character_set_database = utf8"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET character_set_results = utf8"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET character_set_server = utf8"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET character_set_system = utf8"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET collation_server = utf8_general_ci"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET collation_database = utf8_general_ci"
                        //, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET collation_connection = utf8_general_ci"

With the use of "SET NAMES utf8", only the character_set_server is not assigned UTF-8.

使用“SET NAMES utf8”时,只有character_set_server没有分配UTF-8。

, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"


5 => 
    array (size=2)
      'Variable_name' => string 'character_set_server' (length=20)
      'Value' => string 'latin1' (length=6)

When I try to set the collation, he reversed:

当我尝试设置整理时,他反转:

, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET collation_server = utf8_general_ci"

array (size=3)
  0 => 
    array (size=2)
      'Variable_name' => string 'collation_connection' (length=20)
      'Value' => string 'latin1_swedish_ci' (length=17)
  1 => 
    array (size=2)
      'Variable_name' => string 'collation_database' (length=18)
      'Value' => string 'utf8_general_ci' (length=15)
  2 => 
    array (size=2)
      'Variable_name' => string 'collation_server' (length=16)
      'Value' => string 'utf8_general_ci' (length=15)

How to set everyone on UTF-8 in the PDO?

如何在PDO中为每个人设置UTF-8?

1 个解决方案

#1


In general, you rarely need to set anything other than the 3 VARIABLES that SET NAMES changes. Monkeying with the rest of the settings is asking for trouble.

通常,您很少需要设置SET NAMES更改的3个VARIABLES以外的任何内容。使用其他设置进行修改是在寻找麻烦。

To work with utf8, you need

要使用utf8,你需要

  • The bytes in the client need to be utf8-encoded.
  • 客户端中的字节需要进行utf8编码。

  • SET NAMES utf8 (or equivalent)
  • SET NAMES utf8(或同等学历)

  • CHARACTER SET utf8 on the relevant columns
  • CHARACTER在相关列上设置utf8

  • meta tag specifying utf8, if you are using html
  • 指定utf8的元标记,如果您使用的是html

If you are dealing with Chinese, you may want utf8mb4 instead if utf8.

如果您正在处理中文,如果是utf8,您可能需要utf8mb4。

If you end up with some form of garbage (question marks, truncation, mojibake, double-encoding), post another question, including SELECT col, HEX(col) ... to help debug it.

如果您最终得到某种形式的垃圾(问号,截断,mojibake,双重编码),请发布另一个问题,包括SELECT col,HEX(col)...以帮助调试它。

I have not mentioned COLLATION because it is only needed for sorting and comparisons. If you are having trouble with such, provide specifics.

我没有提到COLLATION,因为只需要进行排序和比较。如果您遇到此类问题,请提供详细信息。

#1


In general, you rarely need to set anything other than the 3 VARIABLES that SET NAMES changes. Monkeying with the rest of the settings is asking for trouble.

通常,您很少需要设置SET NAMES更改的3个VARIABLES以外的任何内容。使用其他设置进行修改是在寻找麻烦。

To work with utf8, you need

要使用utf8,你需要

  • The bytes in the client need to be utf8-encoded.
  • 客户端中的字节需要进行utf8编码。

  • SET NAMES utf8 (or equivalent)
  • SET NAMES utf8(或同等学历)

  • CHARACTER SET utf8 on the relevant columns
  • CHARACTER在相关列上设置utf8

  • meta tag specifying utf8, if you are using html
  • 指定utf8的元标记,如果您使用的是html

If you are dealing with Chinese, you may want utf8mb4 instead if utf8.

如果您正在处理中文,如果是utf8,您可能需要utf8mb4。

If you end up with some form of garbage (question marks, truncation, mojibake, double-encoding), post another question, including SELECT col, HEX(col) ... to help debug it.

如果您最终得到某种形式的垃圾(问号,截断,mojibake,双重编码),请发布另一个问题,包括SELECT col,HEX(col)...以帮助调试它。

I have not mentioned COLLATION because it is only needed for sorting and comparisons. If you are having trouble with such, provide specifics.

我没有提到COLLATION,因为只需要进行排序和比较。如果您遇到此类问题,请提供详细信息。