Home  >  Article  >  Backend Development  >  php操作oracle查询时中文乱码,该怎么处理

php操作oracle查询时中文乱码,该怎么处理

WBOY
WBOYOriginal
2016-06-13 13:23:031113browse

php操作oracle查询时中文乱码
环境:
oracle数据库是安装在xp上。
版本:10.2.0.3
NLS_LANGUAGE
AMERICAN

NLS_TERRITORY
AMERICA

NLS_CHARACTERSET
ZHS16GBK

一、sqlplus客户端操作数据库(客户端操作语言环境为SIMPLIFIED CHINESE_CHINA.ZHS16GBK):
1、
cmd
set NLS_LANG=American_America.ZHS16GBK
sqlplus /nolog
conn scott/tiger@salesnew
insert into emp values('7777','你','好','7709','1-11月-2012','5000','800','20');
commit;
select * from emp;

  EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- ------------ ---------- ----------
  DEPTNO
----------
  7777 你 好 7709 01-NOV-12 5000 800
  20
结论:我录入的数据库是没有问题的。

二、php通过oci操作oracle
1、php所以操作及环境为windows 2003 x86 企业版简体中文+iis6.0+oracle instantclient-basic-win32-10.2.0.4.zip+fcgisetup_1.5_rtw_x86.msi
2、配置完所有必需的环境后,测试了
  phpinfo();
?>
显示一切正常
3、开始操作oracle


echo oci_client_version ();

//header('Content-type: text/html; charset=ZHS16GBK');
//set NLS_LANG=American_America.ZHS16GBK
//export NLS_LANG=American_America.ZHS16GBK
//putenv("NLS_LANG=American_America.ZHS16GBK");

$conn = oci_connect('scott', 'tiger', '192.168.1.50/salesnew','ZHS16GBK');
if (!$conn) {
  $e = oci_error();
  trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
   
}
echo oci_server_version ($conn);
// Prepare the statement
$stid = oci_parse($conn, 'SELECT * FROM emp');
if (!$stid) {
  $e = oci_error($conn);
  trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Perform the logic of the query
$r = oci_execute($stid);
if (!$r) {
  $e = oci_error($stid);
  trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Fetch the results of the query
print "

\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
  print "\n";
  foreach ($row as $item) {
  print " \n";
  }
  print "\n";
}
print "
" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "
\n";

oci_free_statement($stid);
oci_close($conn);
echo "测试页面本身中文显示情况";
?> 

中文部分乱码
如下所示:

10.2.0.4.0Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Production With the Partitioning, OLAP and Data Mining options
7777 Ä㠺à 7709 01-NOV-12 5000 800 20 
8888 ÖÓ º£ 7709 01-NOV-10 5000 800 20 
9999 Àî ¾ê 8888 01-JAN-11 3000 5000 20 
7369 SMITH CLERK 7902 17-DEC-80 800 20 
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 
7566 JONES MANAGER 7839 02-APR-81 2975 20 
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 
7782 CLARK MANAGER 7839 09-JUN-81 2450 10 
7788 SCOTT ANALYST 7566 19-APR-87 3000 20 
7839 KING PRESIDENT 17-NOV-81 5000 10 
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 
7876 ADAMS CLERK 7788 23-MAY-87 1100 20 
7900 JAMES CLERK 7698 03-DEC-81 950 30 
7902 FORD ANALYST 7566 03-DEC-81 3000 20 
7934 MILLER CLERK 7782 23-JAN-82 1300 10 
测试页面本身中文显示情况 


这一个问题已经折腾我快疯了,百度、谷歌了好几天都解决不了这个问题。
尝试过以下方法
1、设置php环境所在的操作系统环境变量NLS_LAGN=SIMPLIFIED CHINESE_CHINA.ZHS16GBK或NLS_LANG=American_America.ZHS16GBK。
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