让php支持oracle 10g 数据库

[不指定 2008/11/04 17:46 | by askwan ]
   Oracle对php的支持或者说php对Oracle的支持从Oracle8就开始了,本文主题是记录我在linux系统下编译php以让它可以支持连接Oracle数据库的技术细节。

本文环境为 CentOS4.4 Oracle10gR2 For Linux

假设apache和Oracle已经安装好,且假设Oracle主要的系统环境变量如下:

ORACLE_BASE=/u01/app/oracle;
export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1;
export ORACLE_HOME
ORACLE_SID=newdb;
export ORACLE_SID
ORACLE_TERM=xterm;
export ORACLE_TERM
PATH=/usr/sbin:$PATH;
export PATH
PATH=$ORACLE_HOME/bin:$PATH;
export PATH
export ORACLE_HOME_LISTNER=$ORACLE_HOME
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib;
export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;
export CLASSPATH
export LD_ASSUME_KERNEL


当前shell环境下,解压php,并export出Oracle的几个环境变量,
下面这几个是必须的


export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1  
export ORACLE_SID=newdb
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib


然后编译,这是我的编译选项,根据各自需要稍有不同
Quotation
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-libxml-dir=/usr/local/libxml2 --with-gd=/usr/local/gd2 --with-freetype-dir=/usr/local/freetype/ --with-zlib-dir=/usr/local/zlib/ --with-jpeg-dir=/usr/local/jpeg6/ --with-png-dir --with-ttf --enable-mbstring --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --disable-ipv6 --enable-static --enable-sockets --enable-soap --with-oci8=$ORACLE_HOME


然后一路一次
Quotation
make
make test
make install


下面设置apache对php的支持,在apache配置文件适当位置加入

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps


然后重新启动apache,这时候不出意外情况的话,大概会有一些错误出现,除非你的Oracle安装目录权限给放得太大了。

Quotation
Warning: ocilogon() [function.ocilogon]: OCIEnvNlsCreate() failed. There is something wrong with your system - please check that ORACLE_HOME is set and points to the right director


之所以出现这些错误,这和apache和oracle目录权限有关,这也是我今天遇到的,所以建议启动apache的时候,先export
下Oracle的环境变量,一条一条的export,嫌麻烦的话,写到一个脚本里面吧

vi startapache.sh
Quotation
#!/bin/sh
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1
ORACLE_SID=newdb
export ORACLE_HOME ORACLE_SID
echo "Starting Apache......"
/usr/local/apache2/bin/apachectl start
echo "Starting Apache Sucessful......"
最后一步测试一下,写一个phpinfo页面,如果出现如下段,那就成功一半了
Open in new window

下面测试一下php真实的连接Oracle的情况,下面这个是OTN上弄来的 本人直接“拿来主义了”

测试用户是Oracle的例子用户的scott tiger


<?php

$db_conn = ocilogon( "scott", "tiger","newdb" );

$cmdstr = "select ename, sal from emp";

$parsed = ociparse($db_conn, $cmdstr);
ociexecute($parsed);

$nrows = ocifetchstatement($parsed, $results);

echo "<html><head><title>Oracle PHP For Askwan newdb</title></head><body >";
echo "<center><h2>Oracle PHP For Askwan newdb</h2><br>";
echo "<table border=1 cellspacing='0' width='50%'>\n\<tr>\n";
echo "<td><b>Name</b></td>\n<td><b>Salary</b></td>\n</tr>\n";

for ($i = 0; $i < $nrows; $i++ )
{
    echo "<tr>\n";
    echo "<td>" . $results["ENAME"][$i] . "</td>";
    echo "<td>$ " . number_format($results["SAL"][$i],   2). "</td>";
    echo "</tr>\n";

}

echo "<tr><td colspan='2'> Number of Rows: $nrows</td></tr></table>";
echo "<br><em>If you see data, then it works!</em><br></center></body></html>\n";

?>


如果能看到select的查询输出,那就没有问题了
Open in new window

全文完

作者:askwan@『AskWan』
地址:http://www.askwan.com/post/127/
可以转载,转载时请以链接形式注明作者和原始出处及本声明!

Tags: , , , ,
DataBase | Comments(0) | Trackbacks(0) | Reads(1235)
收藏到网摘:百度搜藏 Google书签 Yahoo收藏 新浪ViVi收藏夹 365天天网摘 天极网摘 我摘·网摘·网络书签 POCO网摘 和讯网摘 Bolaa博拉博客收录中心 天下图摘
Add a comment
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember
Nickname   Password   Optional
Site URI   Email   [Register]