Home  >  Article  >  Backend Development  >  Chinese garbled problem occurs when PHP connects to Oracle

Chinese garbled problem occurs when PHP connects to Oracle

巴扎黑
巴扎黑Original
2016-11-08 11:39:341540browse

The database uses Oracle. When PHP connects to Oracle, if you specify the character set, Chinese garbled characters will appear.

Check the PHP manual, the fourth parameter of oci_pconnect is charset, just set this parameter.

First get Oracle's character set and run "SELECT * FROM V$NLS_PARAMETERS;". The variable NLS_CHARACTERSET corresponds to the character set we need. For example, here it is "AL32UTF8". Therefore, the final PHP code is:

$conn = oci_pconnect('scott','tiger',
"(DEscriptION=(ADDRESS=(PROTOCOL =TCP)(HOST=192.168.0.1)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME=jocsbk)))",
'AL32UTF8');

Since local PHP files use utf-8, direct output is normal. No transcoding is required.

In addition, according to relevant information, if your local file is different from the character set used to connect to Oracle, encoding conversion must be performed.

while ($dat = oci_fetch_row($cur)) {   
  print_r(iconv('gb2312', 'utf-8', $dat[0]));   
}  
//或者
while ($dat = oci_fetch_row($cur)) {   
  print_r(mb_convert_encoding($dat[0], 'utf-8', 'gbk'));   
}

There is another solution, but I haven’t tried it. Oracle can provide data according to the character set specified by the client. In other words, if my local PHP file uses UTF-8, then when I use oci_connect, I can directly specify utf-8, and even the encoding conversion will be saved.

$c1 = oci_connect("scott", "tiger", $db, 'UTF8');


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