php - PDO OCI//PHP中如何设置UTF8

标签 php pdo utf-8 oracle-call-interface

我想连接我的 Oracle SQL 并且已经设置了 utf8, 但当我选择或插入某些内容时仍然出现乱码。

这是我的代码:

define("DB_HOST", "(DESCRIPTION =(ADDRESS = (PROTOCOL=TCP)
                            (HOST= IP)
                            (PORT=1521))
                            (CONNECT_DATA=(SID=IETDB)));
                            charset = utf8
    ");

如何解决这个问题?

最佳答案

您无法通过修改连接字符串来执行此操作,因为不受支持。

oci_connect 中所述手册页,第四个参数指定字符集:

oci_connect($username, $password, $connection_string, 'UTF-8');

这告诉 OCI 希望您提供 UTF8 格式的字符串,并提供从数据库字符集转换而来的 UTF8 格式的结果集。来自手册:

Determines the character set used by the Oracle Client libraries. The character set does not need to match the character set used by the database. If it doesn't match, Oracle will do its best to convert data to and from the database character set. Depending on the character sets this may not give usable results. Conversion also adds some time overhead.

If not specified, the Oracle Client libraries determine a character set from the NLS_LANG environment variable.

Passing this parameter can reduce the time taken to connect.

如果您使用 PDO_OCI,则可以将字符集再次附加到 DSN as documented :

new \PDO('oci:dbname=' . $conn_string . ';charset=UTF8', $username, $password);

关于php - PDO OCI//PHP中如何设置UTF8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43220830/

相关文章:

php - PDO PHP 连接, fatal error

php - Number_format PDO 结果

java - 如何将 UTF-8 字符串转换为日语或任何其他语言的 IText PDF?

c++11 - 在 C++11 中遍历 UTF-8 字符串

php - 在新选项卡/页面中重新加载时保留文本区域的信息

php - 从mysql数据库中选择查询

php - 如何搜索部分/屏蔽字符串?

php - 我数据库中的 "UTF8"数据真的编码正确吗?

php - 单击复选框后启用文本字段

javascript - 使用 javascript 单击后更改按钮的文本