同事昨天接到一個任務,要用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 點多說了
到這裡完成下載兩個zip包,然後就是配置PHP
第三,解壓下載的第一個文件,到電腦任意目錄,解壓後得到文件目錄instantclient_12 _1 建議把後面的"_12_1"去掉,以後改版本就不用動環境變數了
進入這個目錄後雙擊打開adrci.exe
得到如下命令行窗口,證明這個客戶端在你本地可用
複製目錄目錄:devinstantclient 配置到系統的path環境變數,
第四,解壓縮下載的dll的壓縮包,複製其中的 php_oci8.dll php_oci8_11g.dll php_oci8_12c.dll oci8_11g.dll php_oci8_12c.dll 是....../php/ext/目錄下
其實可以不替換,不過建議還是替換一下好,
然後打開php.ini 把擴展打開,最後一個是後來加進去的,php本來沒有,反正上麵包裡有的dll檔都寫進去應該錯不了,頂多運行php的時候多加載幾個庫
extension=php_pdo_oci.dll
extension=php_oci8.dll 膜=php_oci8_11g.dll ; Use with Oracle 11gR2 Instant Client
extension=php_oci8_12c.dll ; Use with Oracle 12c Instant Client
然後重新啟動httpd
enabled
PDO drivers
mysql, oci, odbc |
|
|
OCI8 SupCI abled |
OCI8 Version
2.0.8
| Revision |
$Id: f04114d4d67cffea4cdc2ed3b7f0229c2caa5016 $
|
Oracle Run-time Compient Library Versionsion | 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> (c>$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> ?>
至此,整個的擴充加連接就完成了 |
以上就介紹了自己配置的WAMP環境,擴展oracle函數庫oci,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。 |