kc推理学院10.0网页版推理笔记在哪里拍的玩

PHP5.3连接Oracle客户端及PDO_OCI模块的安装方法
作者:seanlook
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了PHP5.3连接Oracle客户端及PDO_OCI模块的安装方法,结合实例形式详细分析了php5.3环境下PDO_OCI模块的安装方法,并给出了连接Oracle测试程序,需要的朋友可以参考下
本文实例讲述了PHP5.3连接Oracle客户端及PDO_OCI模块的安装方法。分享给大家供大家参考,具体如下:
php连接oracle数据库虽然不是最佳拍档,但组内开发确实有这样需求。如果没有参考合适的文档,这个过程还是挺折磨人的,下面是一个记录,原型是国外的一篇博客 。
假设你已经安装好php的环境,php版本为5.3,要连接的oracle服务器是 11g R2,操作系统版本CentOS 6.4 x86_64。如果没有安装php,可以通过以下命令安装:
# yum install php php-pdo
# yum install php-devel php-pear php-fpm php-gd php-ldap \
php-mbstring php-xml php-xmlrpc php- zlib zlib-devel bc libaio glibc
假如web服务器使用apache。
1. 安装InstantClient
instantclient是oracle的连接数据库的简单客户端,不用安装一个500Moracle客户端就可以连接oracle数据库,有windows和linux版本。从 这里 选择需要的版本下载,只需Basic和Devel两个rpm包。
# rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
# rpm -ivh oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
# ln -s /usr/include/oracle/11.2/client64 /usr/include/oracle/11.2/client
# ln -s /usr/lib/oracle/11.2/client64 /usr/lib/oracle/11.2/client
64位系统需要创建32位的软链接,这里可能是一个遗留bug,不然后面编译会出问题。
接下来还要让系统能够找到oracle客户端的库文件,修改LD_LIBRARY_PATH:
# vi /etc/profile.d/oracle.sh
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
执行source /etc/profile.d/oracle.sh使环境变量生效。
2. 安装PDO_OCI
在连接互联网的情况下,通过pecl在线安装php的扩展非常简单,参考 How to install oracle instantclient and pdo_oci on ubuntu machine 。
从下载 PDO_OCI-1.0.tgz 源文件。
# wget https://pecl.php.net/get/PDO_OCI-1.0.tgz
# tar -xvf PDO_OCI-1.0.tgz
# cd PDO_OCI-1.0
由于PDO_OCI很久没有更新,所以下面需要编辑ODI_OCI-1.0文件夹里的config.m4文件来让它支持11g:
# 在第10行左右找到与下面类似的代码,添加这两行:
elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.11.2; then
PDO_OCI_VERSION=11.2
# 在第101行左右添加这几行:
PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
编译安装pdo_oci扩展:(安装完成后可在 /usr/lib64/php/modules/pdo_oci.so 找到这个模块)
$ ./configure --with-pdo-oci=instantclient,/usr,11.2
$ sudo make install
要启用这个扩展,在/etc/php.d/下新建一个pdo_oci.ini文件,内容:
extension=pdo_oci.so
验证安装成功:
# php -i|grep oci
看到类似下面的内容则安装成功:
/etc/php.d/pdo_oci.ini,
PDO drivers =& oci, sqlite
3. 安装OCI8
从 下载oci8-2.0.8.tgz源文件。
# wget https://pecl.php.net/get/oci8-2.0.8.tgz
# tar -xvf oci8-2.0.8.tgz
# cd oci8-2.0.8
编译安装oci8扩展:
# ./configure --with-oci8=shared,instantclient,/usr/lib/oracle/11.2/client64/lib
# make install
要启用这个扩展,在/etc/php.d/下新建一个oci8.ini文件,内容:
extension=oci8.so
验证安装成功:
# php -i|grep oci8
/etc/php.d/oci8.ini,
oci8.connection_class =& no value =& no value
oci8.default_prefetch =& 100 =& 100
oci8.events =& Off =& Off
oci8.max_persistent =& -1 =& -1
oci8.old_oci_close_semantics =& Off =& Off
oci8.persistent_timeout =& -1 =& -1
oci8.ping_interval =& 60 =& 60
oci8.privileged_connect =& Off =& Off
oci8.statement_cache_size =& 20 =& 20
OLDPWD =& /usr/local/src/oci8-2.0.8
_SERVER["OLDPWD"] =& /usr/local/src/oci8-2.0.8
最后别忘了重启逆web服务器如apache,可以通过phpinfo()来确保扩展是否成功安装。
4. 测试连接
在你web服务器如apache的php目录下创建testoci.php:
$conn = oci_connect('username', 'password', '172.29.88.178/DBTEST');
$stid = oci_parse($conn, 'select table_name from user_tables');
oci_execute($stid);
echo "&table&\n";
while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
echo "&tr&\n";
foreach ($row as $item) {
echo " &td&".($item !== null ? htmlentities($item, ENT_QUOTES) : "&")."&/td&\n";
echo "&/tr&\n";
echo "&/table&\n";
访问这个页面就应该可以得到结果了。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具966,690 四月 独立访问用户
语言 & 开发
架构 & 设计
文化 & 方法
您目前处于:
开放容器项目正式更名为OCI,Oracle等公司加入
开放容器项目正式更名为OCI,Oracle等公司加入
日. 估计阅读时间:
不到一分钟
道AI风控、Serverless架构、EB级存储引擎,尽在!
Author Contacted
相关厂商内容
相关赞助商
ArchSummit深圳-8日,深圳&华侨城洲际酒店,
告诉我们您的想法
允许的HTML标签: a,b,br,blockquote,i,li,pre,u,ul,p
当有人回复此评论时请E-mail通知我
允许的HTML标签: a,b,br,blockquote,i,li,pre,u,ul,p
当有人回复此评论时请E-mail通知我
允许的HTML标签: a,b,br,blockquote,i,li,pre,u,ul,p
当有人回复此评论时请E-mail通知我
赞助商链接
架构 & 设计
文化 & 方法
<及所有内容,版权所有 &#169;
C4Media Inc.
服务器由 提供, 我们最信赖的ISP伙伴。
北京创新网媒广告有限公司
京ICP备号-7
找回密码....
InfoQ账号使用的E-mail
关注你最喜爱的话题和作者
快速浏览网站内你所感兴趣话题的精选内容。
内容自由定制
选择想要阅读的主题和喜爱的作者定制自己的新闻源。
不再错过InfoQ编辑特稿
“当你不知道某件事情的时候,你很难意识到。”想要改变?看看InfoQ编辑们的推荐内容吧。
注意:如果要修改您的邮箱,我们将会发送确认邮件到您原来的邮箱。
使用现有的公司名称
修改公司名称为:
公司性质:
使用现有的公司性质
修改公司性质为:
使用现有的公司规模
修改公司规模为:
使用现在的国家
使用现在的省份
Subscribe to our newsletter?
Subscribe to our industry email notices?
我们发现您在使用ad blocker。
我们理解您使用ad blocker的初衷,但为了保证InfoQ能够继续以免费方式为您服务,我们需要您的支持。InfoQ绝不会在未经您许可的情况下将您的数据提供给第三方。我们仅将其用于向读者发送相关广告内容。请您将InfoQ添加至白名单,感谢您的理解与支持。一条SQL语句,比如select&*&from&&问题如下:
(1)&&可否使用OCI获得结果集中包含的列数
(2)&&能否直接得到记录的行数
有代码最好,恳请大家帮帮忙啊!!!
回复讨论(解决方案)
用OCIAttrGet应该就可以。
通过句柄类型、读取的属性名、属性类型,可以获取到。
sword&OCIAttrGet(&&
dvoid&*trgthndlp,&&//需读取的句柄名&&
ub4&&trghndltyp,&//句柄类型&&
dvoid&*attributep,&//读取的属性名&&
ub4&*sizep,&//属性值长度&&
ub4&attrtype,&&&&&//属性类型&&
OCIError&*errhp&&&//错误句柄&&
具体的代码,我试试看看。等。
Resultset的getColumnListMetaData方法&
The&&&number,&&&types&&&and&&&properties&&&of&&&a&&&ResultSet’s&&&columns&&&are&&&provided&&&by&&&the&
getMetaData&&&method.&&&Returns&&&the&&&description&&&of&&&a&&&ResultSet’s&&&columns.&&&This&
method&&&will&&&return&&&the&&&value&&&of&&&the&&&given&&&column&&&as&&&a&&&PObject.&&&The&&&type&&&of&&&the&
C++&&&object&&&will&&&be&&&the&&&C++&&&PObject&&&type&&&corresponding&&&to&&&the&&&column’s&&&SQL&&&type&
registered&&&with&&&Environment’s&&&map.&&&This&&&method&&&is&&&used&&&to&&&materialize&&&data&&&of&
SQL&&&user-defined&&&types.&
vector&&MetaData&&&&getColumnListMetaData()&&&
用OCIAttrGet应该就可以。
通过句柄类型、读取的属性名、属性类型,可以获取到。
sword&OCIAttrGet(&&
dvoid&*trgthndlp,&//需读取的句柄名&&
ub4&trghndltyp,&//句柄类型&&
dvoid&*attributep,&//读取的属性名&&
ub4&*sizep,&//属性值长度&&
ub4&attrtype,&//属性类型&&……
OCIError&*errhp&//错误句柄
(1)可否使用OCI获得结果集中包含的列数
语句句柄=OCI_HTYPE_STMT,&属性类型=OCI_ATTR_PARAM_COUNT
OCIAttrGet(smuhp,OCI_HTYPE_STMT,(dvoid&*)&&numcols,(ub4*)0,
(ub4)OCI_ATTR_PARAM_COUNT,errhp);
(2)返回结果集中的行数
语句句柄=OCI_HTYPE_STMT,&属性类型=OCI_ATTR_ROW_COUNT
OCIAttrGet(stmhp,OCI_HTYPE_STMT,(dvoid*)&&rOWS,0,
(ub4)&OCI_ATTR_ROW_COUNT,errhp);&#65279;&#65279;
连接两种方式thin与oci区别
前几天同事跑过来跟我说,&机房中的一台tomcat服务器跟oracle机连接很慢,查看控制台中的日志,&基本上是一条sql出来要等个1-2秒再出第二条。但同样的程序在他自己机器上的tomcat运行,同样是连那台数据库机器,就快很多,不会出现前面的每&执行1条sql就卡一次壳的情况。&
初步分析,我就想到可能是网络原因,&机房两台机器连接不畅通,&程序和机器差的原因基本可以排除,&机房的tomcat机比我们开发机要强多了,&而且程序在他的机器上运行又没有问题。于是我就劝他到机房去检查一下网络状态,&但他一时也无法进入,因为机房的管理人员不在。&
过了一会,&他告诉我问题解决了,&把数据库访问的url更换成了oci方式就好了,&oci对我来说有些陌生,&我一直是用的thin,也没想过其他连接方式。对于oci我也只能想到oracle&的client中貌&#20284;是有oci什么的,当时有其他事情也没管了。&
今天有意了解一下区别,先看看thin和oci的url写法上的区别:&
jdbc:oracle:thin:@server&ip:&service&
jdbc:oracle:oci:@service&
看来oci的还更加简洁,ip可以省掉不写了。&
接下来再找找oci和thin的其他区别,发现有如下解释:&
Oracle&provides&four&different&types&of&JDBC&drivers,&for&use&in&different&deployment&scenarios.&The&10.1.0&drivers&can&access&Oracle&8.1.7&and&higher.&While&all&Oracle&JDBC&drivers&are&similar,&some&features&apply&only&to&JDBC&OCI&drivers&and&some&apply&only&to&the&JDBC&Thin&driver.&
JDBC&OCI&client-side&driver:&This&is&a&JDBC&Type&2&driver&that&uses&Java&native&methods&to&call&entrypoints&in&an&underlying&C&library.&That&C&library,&called&OCI&(Oracle&Call&Interface),&interacts&with&an&Oracle&database.&The&JDBC&OCI&driver&requires&an&Oracle&client&installation&of&the&same&version&as&the&driver.&
The&use&of&native&methods&makes&the&JDBC&OCI&driver&platform&specific.&Oracle&supports&Solaris,&Windows,&and&many&other&platforms.&This&means&that&the&Oracle&JDBC&OCI&driver&is&not&appropriate&for&Java&applets,&because&it&depends&on&a&C&library.&
Starting&from&10.1.0,&the&JDBC&OCI&driver&is&available&for&install&with&the&OCI&Instant&Client&feature,&which&does&not&require&a&complete&Oracle&client-installation.&Please&refer&to&Oracle&Call&Interface&for&more&information.&
JDBC&Thin&client-side&driver:&This&is&a&JDBC&Type&4&driver&that&uses&Java&to&connect&directly&to&Oracle.&It&implements&Oracle's&SQL*Net&Net8&and&TTC&adapters&using&its&own&TCP/IP&based&Java&socket&implementation.&The&JDBC&Thin&driver&does&not&require&Oracle&client&software&to&be&installed,&but&does&require&the&server&to&be&configured&with&a&TCP/IP&listener.&
Because&it&is&written&entirely&in&Java,&this&driver&is&platform-independent.&The&JDBC&Thin&driver&can&be&downloaded&into&any&browser&as&part&of&a&Java&application.&(Note&that&if&running&in&a&client&browser,&that&browser&must&allow&the&applet&to&open&a&Java&socket&connection&back&to&the&server.)&
JDBC&Thin&server-side&driver:&This&is&another&JDBC&Type&4&driver&that&uses&Java&to&connect&directly&to&Oracle.&This&driver&is&used&internally&within&the&Oracle&database.&This&driver&offers&the&same&functionality&as&the&client-side&JDBC&Thin&driver&(above),&but&runs&inside&an&Oracle&database&and&is&used&to&access&remote&databases.&
Because&it&is&written&entirely&in&Java,&this&driver&is&platform-independent.&There&is&no&difference&in&your&code&between&using&the&Thin&driver&from&a&client&application&or&from&inside&a&server.&
连接方式有以下几种:&
Oralce&provides&four&types&of&JDBC&driver.&
Thin&Driver,&a&100%&Java&driver&for&client-side&use&without&an&Oracle&installation,&particularly&with&applets.&The&Thin&driver&type&is&thin.&To&connect&user&scott&with&password&tiger&to&a&database&with&SID&(system&identifier)&orcl&through&port&1521&of&host&myhost,&using&the&Thin&driver,&you&would&write&:&
Connection&conn&=&DriverManager.getConnection&
(&jdbc:oracle:thin:@myhost:1521:orcl&,&&scott&,&&tiger&);&
OCI&Driver&for&client-side&use&with&an&Oracle&client&installation.&The&OCI&driver&type&is&oci.&To&connect&user&scott&with&password&tiger&to&a&database&with&SID&(system&identifier)&orcl&through&port&1521&of&host&myhost,&using&the&OCI&driver,&you&would&write&:&
Connection&conn&=&DriverManager.getConnection&
(&jdbc:oracle:oci:@myhost:1521:orcl&,&&scott&,&&tiger&);&
Note&that&you&can&also&specify&the&database&by&a&TNSNAMES&entry.&You&can&find&the&available&TNSNAMES&entries&listed&in&the&file&tnsnames.ora&on&the&client&computer&from&which&you&are&connecting.&For&example,&if&you&want&to&connect&to&the&database&on&host&myhost&as&user&scott&with&password&tiger&that&has&a&TNSNAMES&entry&of&MyHostString,&enter:&
Connection&conn&=&DriverManager.getConnection&
(&jdbc:oracle:oci8:@MyHostString&,&scott&,&tiger&);&
If&your&JDBC&client&and&Oracle&server&are&running&on&the&same&machine,&the&OCI&driver&can&use&IPC&(InterProcess&Communication)&to&connect&to&the&database&instead&of&a&network&connection.&An&IPC&connection&is&much&faster&than&a&network&connection.&
Connection&conn&=&DriverManager.getConnection&
(&jdbc:oracle:oci8:@&,&scott&,&tiger&);&
Server-Side&Thin&Driver,&which&is&functionally&the&same&as&the&client-side&Thin&driver,&but&is&for&code&that&runs&inside&an&Oracle&server&and&needs&to&access&a&remote&server,&including&middle-tier&scenarios.&The&Server-Side&Thin&driver&type&is&thin&and&there&is&no&difference&in&your&code&between&using&the&Thin&driver&from&a&client&application&or&from&inside&a&server.&
Server-Side&Internal&Driver&for&code&that&runs&inside&the&target&server,&that&is,&inside&the&Oracle&server&that&it&must&access.&The&Server-Side&Internal&driver&type&is&kprb&and&it&actually&runs&within&a&default&session.&You&are&already&&connected&.&Therefore&the&connection&should&never&be&closed.&
To&access&the&default&connection,&write:&
DriverManager.getConnection(&jdbc:oracle:kprb:&);&
DriverManager.getConnection(&jdbc:default:connection:&);&
You&can&also&use&the&Oracle-specific&defaultConnection()&method&of&the&OracleDriver&class&which&is&generally&recommended:&
OracleDriver&ora&=&new&OracleDriver();&
Connection&conn&=&ora.defaultConnection();&
Note:&You&are&no&longer&required&to&register&the&OracleDriver&class&for&connecting&with&the&Server-Side&Internal&driver,&although&there&is&no&harm&in&doing&so.&This&is&true&whether&you&are&using&getConnection()&or&defaultConnection()&to&make&the&connection.&
Any&user&name&or&password&you&include&in&the&URL&string&is&ignored&in&connecting&to&the&server&default&connection.&The&DriverManager.getConnection()&method&returns&a&new&Java&Connection&object&every&time&you&call&it.&Note&that&although&the&method&is&not&creating&a&new&physical&connection&(only&a&single&implicit&connection&is&used),&it&is&returning&a&new&object.&
Again,&when&JDBC&code&is&running&inside&the&target&server,&the&connection&is&an&implicit&data&channel,&not&an&explicit&connection&instance&as&from&a&client.&It&should&never&be&closed.&
这下基本明白了&
1)从使用上来说,oci必须在客户机上安装oracle客户端或才能连接,而thin就不需要,因此从使用上来讲thin还是更加方便,这也是thin比较常见的原因。&
2)原理上来看,thin是纯java实现tcp/ip的c/s通讯;而oci方式,客户端通过native&java&method调用c&library访问服务端,而这个c&library就是oci(oracle&called&interface),因此这个oci总是需要随着oracle客户端安装(从oracle10.1.0开始,单独提供OCI&Instant&Client,不用再完整的安装client)&
3)它们分别是不同的驱动类别,oci是二类驱动,&thin是四类驱动,但它们在功能上并无差异。&
4)虽然很多人说oci的速度快于thin,但找了半天没有找到相关的报告。
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:9220次
排名:千里之外
原创:11篇
转载:10篇
(1)(6)(6)(8)

我要回帖

更多关于 侦探推理网页游戏 的文章

 

随机推荐