gpt4 book ai didi

php - SET 排序规则和字符 UTF8 - Mysql PDO

转载 作者:行者123 更新时间:2023-11-29 22:28:55 26 4
gpt4 key购买 nike

我想了解以下内容:当您输入 mysql 时,输入以下命令:

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

它返回排序规则字符,因此我配置为在这种情况下让它们采用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)

如何像这样使用 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"

使用“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)

当我尝试设置排序规则时,他反转了:

, \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)

如何在 PDO 中将所有人设置为 UTF-8

最佳答案

一般来说,除了 SET NAMES 更改的 3 个变量之外,您很少需要设置任何其他内容。对其余设置进行胡闹就是自找麻烦。

要使用 utf8,您需要

  • 客户端中的字节需要进行utf8编码。
  • 设置名称 utf8(或等效项)
  • 相关列上的字符集 utf8
  • meta 标记指定 utf8(如果您使用的是 html)

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

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

我没有提到COLLATION,因为它仅用于排序和比较。如果您遇到此类问题,请提供具体信息。

关于php - SET 排序规则和字符 UTF8 - Mysql PDO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30004309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com