Oracle Database XEをphpからCRUD(CREATE,READ,UPDATE,DELETE)を実行しました。

Oracle Database XEをphpから実行する為に色々調べました。

標準関数で出来ないかなと探したのですが、なかなか見つからず…。

OTNのZend Core for Oracleというのをインストールしてみたのですが、インストールに失敗してphp.iniがおかしな事になってしまいました!

php.iniのバックアップしておけば良かったと後悔。(php5.2.6になってるし…)

ZendCoreについて
http://www.oracle.com/technology/tech/php/zendcore/index.html

結局元に戻す方法がわからなかったので、OSからインストール。
その後、Oracle Database XEをインストールし直しました。


http://www.oracle.com/technology/global/jp/tech/php/htdocs/php-oracle-tutorial.html

上記のチュートリアルに紹介されているoci8関数をインストールしても、oracleへ接続出来そうなので、
peclでoci8関数をインストールしました。

pecl install oci8

oci8関数をインストール後、phpから利用する為に、
php.iniにextension=oci8.soを追加しapacheを再起動しました。

/var/www/html/test.phpを作成。(チュートリアル丸写し)

<?php
  $conn = oci_connect('hr', 'hrpw', '//localhost/XE');

  $stid = oci_parse($conn, 'select city from locations');
  oci_execute($stid);

  print '<table border="1">';
  while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
    print '<tr>';
    foreach ($row as $item) {
      print '<td>'.($item?htmlentities($item):' ').'</td>';
    }
    print '</tr>';
  }
  print '</table>';
?> 

コマンドラインから実行すると、
環境変数ORACLE_HOMEの値がないというエラーが表示されました。

php test.php

PHP Warning:  oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that ORACLE_HOME is set and points to the right directory in /var/www/html/test.php on line 2
PHP Warning:  oci_parse() expects parameter 1 to be resource, boolean given in /var/www/html/test.php on line 4
PHP Warning:  oci_execute() expects parameter 1 to be resource, null given in /var/www/html/test.php on line 5

なので.bashrcに以下を記述

. /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
source .bashrc

そしてコマンドラインから再度phpを実行

php test.php

<table border="1"><tr><td>city1</td><td>city1</td></tr></table>

やっとSQLを実行できました。

しかし、ブラウザからアクセスするとまだ真っ白の画面が表示され、エラーログに下記が記述されていました。

tail /var/log/httpd/error_log

[Wed Jan 21 08:44:44 2009] [error] [client 192.168.11.8] PHP Warning:  oci_connect() [<a href='function.oci-connect'>function.oci-connect</a>]: OCIEnvNlsCreate() failed. There is something wrong with your system - please check that ORACLE_HOME is set and points to the right directory in /var/www/html/test.php on line 2
[Wed Jan 21 08:44:44 2009] [error] [client 192.168.11.8] PHP Warning:  oci_parse() expects parameter 1 to be resource, boolean given in /var/www/html/test.php on line 5
[Wed Jan 21 08:44:44 2009] [error] [client 192.168.11.8] PHP Warning:  oci_execute() expects parameter 1 to be resource, null given in /var/www/html/test.php on line 6
[Wed Jan 21 08:44:44 2009] [error] [client 192.168.11.8] PHP Warning:  oci_fetch_array() expects parameter 1 to be resource, null given in /var/www/html/test.php on line 9

apache経由だと環境変数ORACLE_HOMEが定義されていないようです。

かなり強引ですが、/etc/init.d/httpdのstart時に無理やり定義してみました。

vi /etc/init.d/httpd

start() {
       
        ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server

        echo -n $"Starting $prog: "
        check13 || exit 1
        LANG=$HTTPD_LANG daemon $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

これでやっとブラウザ経由でSQLを実行する事が確認できました。



[PR]Spreeの情報を集めています。

ECを持ちたい方、仕事でECを使いたい方向けのコミュニティサイトです。
このサイトでは世界で最も使用されているECの1つであるSpreeについての情報を提供しています。
http://spreecommerce.jp/