gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:57:47 26 4
gpt4 key购买 nike

我想连接我的 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/

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