PHP 連接 Oracle - OCI8

    系統環境:

    • CentOS 5.5 32bit
    • PHP 5.1.6/5.3.5
    • Oracle 9.2.0.6

    安裝/升級 PHP 5.3

    此項非必要,若需要請參閱文章 CentOS 5.5 升級至 PHP 5.3

    安裝 Oracle Instant Client

    // 下載位址
    http://www.oracle.com/technetwork/da...ex-097480.html

    1. Instant Client for Linux x86 > 選擇 Accept License Agreement,及 oracle-instantclient-basic-10.2.0.4-1.i386.rpm
    2. Instant Client for Linux x86 > 選擇 Accept License Agreement,及 oracle-instantclient-devel-10.2.0.4-1.i386.rpm

    注意:檔案約 53.9MB,需要登入帳號才能下載。

    // 開始安裝

    cd /usr/src
    rpm -ivh oracle-instantclient-basic-10.2.0.4-1.i386.rpm
    rpm -ivh oracle-instantclient-devel-10.2.0.4-1.i386.rpm

    安裝 PECL

    // 以 PHP 5.3 為例
    // 安裝 php53-pear 模組

    cd /usr/src/redhat/SRPMS/
    wget http://dl.iuscommunity.org/pub/ius/archive/Redhat/5.4/SRPMS/php53-pear-1.8.1-5.ius.el5.src.rpm
    rpmbuild --rebuild php53-pear-1.8.1-5.ius.el5.src.rpm
    rpm -ivh /usr/src/redhat/RPMS/noarch/php53-pear-1.8.1-5.ius.noarch.rpm
    

    NOTEs:

    *  在 PHP 5.3 ,不能使用 yum 安裝 php-pear。 

    Rebuild PHP RPMs

    for PHP 5.1.6(built-in)

    下載 SRPM

    cd /usr/src/redhat/SRPMS
    wget http://ftp.isu.edu.tw/pub/Linux/CentOS/5.5/os/SRPMS/php-5.1.6-27.el5.src.rpm 
    rpm -ivh php-5.1.6-27.el5.src.rpm
    vi ../SPECS/php.spec
    

    php.spec: 找到這幾行

    # Build /usr/bin/php-cgi with the CGI SAPI, and all the shared extensions
    pushd build-cgi
    build --enable-force-cgi-redirect \
          --enable-pcntl \
          --with-imap=shared --with-imap-ssl \
          --enable-mbstring=shared --enable-mbstr-enc-trans \
          --enable-mbregex \
          --with-ncurses=shared \
          --with-gd=shared \
          --enable-bcmath=shared \
          --enable-dba=shared --with-db4=%{_prefix} \
          --with-xmlrpc=shared \
          --with-ldap=shared --with-ldap-sasl \
          --with-mysql=shared,%{_prefix} \
          --with-mysqli=shared,%{_libdir}/mysql/mysql_config \
          --enable-dom=shared \
          --with-dom-xslt=%{_prefix} --with-dom-exslt=%{_prefix} \
          --with-pgsql=shared \
          --with-snmp=shared,%{_prefix} \
          --enable-soap=shared \
          --with-xsl=shared,%{_prefix} \
          --enable-xmlreader=shared --enable-xmlwriter=shared \
          --enable-fastcgi \
          --enable-pdo=shared \
          --with-pdo-odbc=shared,unixODBC,%{_prefix} \
          --with-pdo-mysql=shared,%{_libdir}/mysql/mysql_config \
          --with-pdo-pgsql=shared,%{_prefix} \
          --with-pdo-sqlite=shared,%{_prefix} \
          --enable-dbase=shared
    

    加上一行

    --with-oci8=instantclient,/usr/lib/oracle/10.2.0.4/client/lib \
    --enable-dbase=shared
    

    開始編譯

    rpmbuild -bb ../SPECS/php.spec
    

    安裝 oci8.so

    cd ../BUILD/php-5.1.6/ext/oci8/
    phpize
    ./configure --with-oci8=shared,instantclient,/usr/lib/oracle/10.2.0.4/client/lib
    make
    make install
    vi /etc/php.d/oci8.ini

    oci8.ini:

    ; Enable oci8 extension module
    extension=oci8.so
    

    // 重啟 Apache

    service httpd restart

    檢查 phpinfo

    php_oci8.png

    for PHP 5.3

    預設的 PHP RPM 安裝並不支援 oci8 及 pdo_oci extension,所以必須重編 PHP,然後再重裝一次。

    // 從網路或 CD 找出現有 PHP 版本的 SRPM,並安裝它。

    rpm -ivh php53-5.3.5-1.el5.src.rpm
    

    // 修改 php53.spec
    /usr/src/redhat/SPECS/php53.spec,找到這幾行

    # Build /usr/bin/php-cgi with the CGI SAPI, and all the shared extensions
    pushd build-cgi
    build --enable-pcntl \
          --with-imap=shared --with-imap-ssl \
          --enable-mbstring=shared \
          --enable-mbregex \
          --with-gd=shared \
          --enable-bcmath=shared \
          --enable-dba=shared --with-db4=%{_prefix} \
          --with-xmlrpc=shared \
          --with-ldap=shared --with-ldap-sasl \
          --with-mysql=shared,%{_prefix} \
          --with-mysqli=shared,%{_bindir}/mysql_config \
          --enable-dom=shared \
          --with-pgsql=shared \
          --with-snmp=shared,%{_prefix} \
          --enable-soap=shared \
          --with-xsl=shared,%{_prefix} \
          --enable-xmlreader=shared --enable-xmlwriter=shared \
          --enable-pdo=shared \
          --with-pdo-odbc=shared,unixODBC,%{_prefix} \
          --with-pdo-mysql=shared,%{_prefix} \
          --with-pdo-pgsql=shared,%{_prefix} \
          --with-pdo-sqlite=shared,%{_prefix}

    // 在下方加上一行參數

    --with-pdo-sqlite=shared,%{_prefix} \
    --with-oci8=instantclient,/usr/lib/oracle/11.2/client/lib
    

    // 重新編譯

    cd /usr/src/redhat/SPECS/
    rpmbuild -bb php53.spec

    以下步驟參考 php 5.1.6 作法。

    網頁測試

    oracle-test.php:

    <?php
    
    $conn = oci_connect('dbuser', 'dbpass', 'ORACLE.SERVER.HOST.NAME/SERVICENAME');
    
    $stid = oci_parse($conn, 'select * from TABLENAME');
    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) : "&nbsp;")."</td>\n";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
    
    ?>
    

     

     

    F.A.Q

    Q: 編譯時遭遇錯誤:checking for oci.h... configure: error: I'm too dumb to figure out where the include dir is in your Instant Client install
    Ans: 請檢查編譯參數 --with-oci8, --with-pdo-oci 的路徑是否正確。

    Q: 編譯時遭遇錯誤:make: *** [ext/ldap/ldap.lo] Error 1?
    Ans: 請更換為 Oracle Instant Client 10.2.0.4。

    Q: 編譯時遭遇錯誤:configure: error: pcntl: fork() not supported by this platform?
    Ans: 執行 export LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.4/client/lib

    Q: 編譯時遭遇錯誤:
    configure: error:
    You've configured extension pdo_oci, which depends on extension pdo,
    but you've either not enabled pdo, or have disabled it.
    Ans: 移除編譯參數 --disable-pdo

    Q: 編譯時遭遇錯誤: Installed (but unpackaged) file(s) found: /usr/lib/php/modules/pdo_oci.so
    Ans:

    標籤 (Edit tags)
    • No tags

    文件 1

    文件大小日期附件上傳者 
     php_oci8.png
    PHP OCI8 Module
    15.64 KB11:02, 18 Mar 2011alang動作
    您必須 登入 才能發佈評論。
    Powered by MindTouch Core