Home  >  Article  >  php教程  >  自己配置的WAMP环境,扩展oracle函数库(oci),wampoci

自己配置的WAMP环境,扩展oracle函数库(oci),wampoci

WBOY
WBOYOriginal
2016-06-13 08:59:581162browse

自己配置的WAMP环境,扩展oracle函数库(oci),wampoci

同事昨天接到一个任务,要用php处理oracle数据库的内容,但是php打开oracle扩展不是像mysql那样直接用就行,需要下一点东西才能打开

第一步 需要到oracle官方下载一个install client 包,在win下找到你对应系统版本的zip(注意这里是系统版本)

截止到2015-06-25,下载地址如下http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

例如选择 Instant Client for Microsoft Windows (x64)  因为php扩展的是OCI,所以必须选对应版本的最全的那个,别的都没有OCI,这里下载可能需要有个账号,验证以后就可以下载,直接用连接下载不好用,没有的话注册一下就好了

 

第二部,需要下载win系统中的扩展包也就是.dll文件  下载地址   http://pecl.php.net/package/oci8   下面一段废话是告诉你怎么找的

http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 在这个地址下点击

official Instant Client site.  拉到最后 Related Developer Centers 里面点     "PHP - OCI8 extension"

然后选项卡 downloads  点OCI8  到   http://pecl.php.net/package/oci8  

然后怎么下载就不多说了

 

到这里完成下载两个zip包,然后就是配置PHP

第三,解压下载的第一个文件,到电脑任意目录,解压后得到文件目录 instantclient_12 _1  建议把后面的 "_12_1"去掉,以后改版本就不用动环境变量了

进入这个目录后双击打开 adrci.exe

得到如下命令行窗口,证明这个客户端在你本地可用

复制目录,我的是  F:\dev\instantclient  配置到系统的path环境变量,

 

第四,解压下载的dll的压缩包,复制其中的  php_oci8.dll  php_oci8_11g.dll  php_oci8_12c.dll  我目前就是这三个,到php的扩展包文件,通常是 ....../php/ext/目录下

其实可以不替换,不过建议还是替换一下好,

然后打开php.ini 把扩展打开,最后一个是后来加进去的,php本来没有,反正上面包里有的dll文件都写进去应该错不了,顶多运行php的时候多加载几个库

extension=php_pdo_oci.dll

extension=php_oci8.dll      ; Use with Oracle 10gR2 Instant Client
extension=php_oci8_11g.dll  ; Use with Oracle 11gR2 Instant Client
extension=php_oci8_12c.dll  ; Use with Oracle 12c   Instant Client

然后重启httpd服务,打开 phpinfo(); 应该能看到

PDO support enabled
PDO drivers mysql, oci, odbc

 

OCI8 Support enabled
OCI8 DTrace Support disabled
OCI8 Version 2.0.8
Revision $Id: f04114d4d67cffea4cdc2ed3b7f0229c2caa5016 $
Oracle Run-time Client Library Version 12.1.0.2.0
Oracle Compile-time Instant Client Version 10.2

这样就说明扩展打开成功了

 

最后就是连接oracle数据库了

这里给一个实例连接 原文连接 http://www.orczhou.com/index.php/2010/09/php-oci8-oracle/

<span> 1</span> <?<span>php  
</span><span> 2</span> <span>//</span><span>配置信息</span>
<span> 3</span> <span>$ora_host</span> = "172.16.1.150"<span>;
</span><span> 4</span> <span>$ora_port</span>="1521"<span>;
</span><span> 5</span> <span>$ora_sid</span> = "cop"<span>;
</span><span> 6</span> <span>$ora_username</span> = "webdev"<span>;
</span><span> 7</span> <span>$ora_password</span> = "webdev"<span>;
</span><span> 8</span> <span>$charset</span> = "UTF8"; <span>#</span><span>## zhs16gbk ###
</span><span> 9</span> 
<span>10</span> <span>//构建Easy Connect string
</span><span>11</span> <span>//(如果tnsnames.ora中已经有了,可以直接使用Connect Name)</span>
<span>12</span> <span>$ora_connstr</span> = "<span>(description=(address=(protocol=tcp)
</span><span>13</span> (host=".<span>$ora_host</span>.")(port=".<span>$ora_port</span>."<span>))
</span><span>14</span> (connect_data=(service_name=".<span>$ora_sid</span>.")))"<span>;
</span><span>15</span> <span>//</span><span>连接数据库</span>
<span>16</span> <span>$conn</span> = oci_connect(<span>$ora_username</span>, <span>$ora_password</span>,<span>$ora_connstr</span><span>);
</span><span>17</span> <span>//</span><span>执行Query,这里$res接收的是一个boolean值</span>
<span>18</span> <span>$stid</span> = oci_parse(<span>$conn</span>, 'select * FROM dq_try_app'<span>);
</span><span>19</span> <span>$res</span> = oci_execute(<span>$stid</span><span>);
</span><span>20</span> 
<span>21</span> <span>//</span><span>在while循环中使用oci_fetch_array遍历结果。</span>
<span>22</span> <span>while</span> (<span>$row</span> = oci_fetch_array(<span>$stid</span>, OCI_ASSOC+<span>OCI_RETURN_NULLS)) {
</span><span>23</span>     
<span>24</span>     <span>foreach</span> (<span>$row</span> <span>as</span> <span>$item</span><span>) {
</span><span>25</span>         <span>echo</span> <span>$item</span>."***"<span>;
</span><span>26</span> <span>    }
</span><span>27</span>     <span>echo</span> "<br>"<span>;
</span><span>28</span>     
<span>29</span> <span>}
</span><span>30</span> <span>//</span><span>var_dump($stid);   </span>
<span>31</span> ?>

至此,整个的扩展加连接就完成了

 

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