Home >Backend Development >PHP Problem >How to solve the garbled problem of php connection to oracle

How to solve the garbled problem of php connection to oracle

藏色散人
藏色散人Original
2021-10-27 10:43:293241browse

Solution to garbled php connection to oracle: 1. Set environment variables; 2. Get the character set of oracle; 3. Pass "iconv('GBK','utf-8',$vo["USERNAME" ]);" method to convert the encoding.

How to solve the garbled problem of php connection to oracle

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to solve the php connection oracle garbled problem?

Notes on PHP connection to Oracle and garbled characters

1. PHP connection to Oracle

Step 1. Extract Oracle Instant Client Core DLL

Download the Instant Client Basic (11g) package for Windows from OTN's Instant Client page. The size of this compressed file is approximately 48MB. Create a subdirectory (for example, c:\instantclient11_2) and copy the following libraries from the compressed file to the apache\bin directory:

oraociei11.dll 
orannzsbb11.dll 
oci.dll

The total size of these three files is approximately 126MB.
To use the "oracle" extension for older versions of PHP (enabled using "extension=php_oracle.dll" in php.ini), copy ociw32.dll instead of oci.dll.

Step 2: Edit the environment variables and add c:\instantclient11_2 to PATH (the system environment variables are located before other Oracle directories).

For example, on Win7, click "Computer"->right-click "Properties"->"Advanced System Settings"->"Advanced"->"Environment Variables" to edit system variables PATH in the list.
If the tnsnames.ora file is used to define the Oracle Net service name, copy tnsnames.ora to c:\instantclient11_2 and set the user environment variable TNS_ADMIN to c:\instantclient11_2.

Step 3: Open the oci8 extension of php. Edit php.ini and open the OCI8 extension, that is, remove the comment symbol ‘;’: extension=php_oci8.dll

Restart Apache. Restart the server (the server must be restarted, otherwise the environment variables will not take effect)

After restarting, if you see the following content through phpinfo(), the configuration is successful:

oci8

##Active Persistent Connections0Active Connections0Oracle Run-time Client Library Version11.2.0.3.0Oracle Instant Client Version11.2Temporary Lob supportenabledCollections support enabled
OCI8 Support enabled
Version 1.4.7
Revision $Id: bf2eaf558b050b6d2e6d098bed6345af7e842ea4 $
##Directiveoci8.connection_classno valueno valueoci8.default_prefetch oci8.eventsoci8.max_persistent##oci8.old_oci_close_semanticsOffOffoci8.persistent_timeout-1-160Off20
Local Value Master Value
100 100
Off Off
-1 -1
##oci8.ping_interval
60 oci8.privileged_connect
Off oci8.statement_cache_size
20

 2. PHP Oracle 中文乱码问题

通常缺省配置连接Oracle在处理中文时都会遇到乱码问题,其实绝大部分人都知道在客户端连接Oracle服务端前首先要在客户端正确的设置服务端的字符集信息,通过PLSQL运行“select * from V$NLS_PARAMETERS;” 即可获取oracle的字符集,变量NLS_CHARACTERSET对应的就是我们需要的字符集,比如我这里就是“WE8ISO8859P1”

设置字符集的方法如下:

   方法一: 连接前设置环境变量            

putenv("NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1");
$conn=oci_new_connect($config['username'], $config['password'],$config['database']);

方法二:连接时设置环境变量

 $conn=oci_new_connect($config['username'], $config['password'],$config['database'],'we8iso8859p1');

    但是很快你会发现通过上述设置读取的中文数据在不设置编码的情况下可以正常显示,而一旦在页面(假如页面的字符集为UTF8)中使用则仍为乱码,

 并且即便做转换 从 we8iso8859p1 -> utf-8  依旧为乱码。

     其实仔细研究后发现oci8 以数据库编码WE8ISO8859P1获取数据后 自动转换为操作系统缺省的编码格式,假如我使用的操作系统缺省编码为GBK,则实际上通过OCI8读取后,字符的编码即为GBK, 因此在页面使用的时候编码转换应该为  从 GBK -> utf-8 :

 echo iconv('GBK','utf-8',$vo["USERNAME"]);

推荐学习:《PHP视频教程

The above is the detailed content of How to solve the garbled problem of php connection to oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn