PDO相当于mysql_client_encoding()? [英] PDO equivalent of mysql_client_encoding()?

查看:177
本文介绍了PDO相当于mysql_client_encoding()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法在PDO中检查客户端编码,就像在mysql / mysqli中有 mysql_client_encoding();

Is there any way in PDO to check the client encoding, like there was in mysql/mysqli with mysql_client_encoding();?

在PHP.net它说,可以设置字符集与 PDO :: setAttribute(),例如:

On PHP.net it states that one can set the charset with PDO::setAttribute(), e.g.:

$db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");

但是如何获取当前字符集?

But how does one get the current charset?

推荐答案

有两种不同的字符集:

  • 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);

这篇关于PDO相当于mysql_client_encoding()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆