且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PDO相当于mysql_client_encoding()?

更新时间:2022-04-28 04:54:00

有两种不同的字符集:

  • the encoding in which MySQL assumes strings are sent by the client (character_set_client); and
  • the encoding in which MySQL will send its responses (character_set_results).

的这些变量使用PDO,您可以获取相关的 显示变量 语句;例如:

To ascertain the current value of these variables using PDO, you could fetch the results of the relevant SHOW VARIABLES statement; for example:

$qry = $db->query("SHOW VARIABLES LIKE 'character_set_client'");

mysql_client_encoding() 有点含糊,因为它指出:

The documentation for mysql_client_encoding() is somewhat ambiguous, as it states:


从MySQL中检索 character_set 变量。

但是,没有这样的服务器系统变量存在:所以我不知道它会返回什么。

However, no such server system variable exists: so I'm not sure which it would return.

最后,不是设置 MYSQL_ATTR_INIT_COMMAND ,您可以在DSN中指定所需的字符集(如在手册):

Finally, rather than setting a MYSQL_ATTR_INIT_COMMAND, you can specify your desired character set in the DSN (as mentioned in the manual):

$db = new PDO("mysql:dbname=$db;host=$host;charset=$charset", $user, $password);