• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-23) 15次浏览 已收录 0个评论

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、配置完所有必需的环境后,测试了
<?php
phpinfo();
?>
显示一切正常
3、开始操作oracle

<?php
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 MANA本文来源gaodai#ma#com搞*!代#%^码$网!搞代gaodaima码GER 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。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php操作oracle查询时中文乱码,该怎么处理

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址