实现PHP程序访问oracle DB
时间:2014.09.02
写作者:刘群英(qunyingliu)
环境描述:apache版本,PHP版本,oracle DB版本,linux发行版本
RPM包安装oracle-instantclient客户端
源码编译安装oci8扩展
系统环境与php配置修改:ld.so.conf.d目录配置文件与php.ini中扩展字段信息修改
重启apache使修改生效
测试链接代码,确认配置成功
环境描述:
本地系统:centos linux 6.4 x64
apache 2.2.22:/usr/local/apache2
PHP 5.4 : /usr/local/php
oracle DB 11.2.0.4
RPM包安装本地oracle client:
从[oracle Instant Client 官网 下载以下RPM包:
http://www.oracle.com/technetwork/cn/topics/linuxx86-64soft-092277.html
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
使用以下命令进行安装:
rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
源码编译安装oci8扩展下载:
wget http://pecl.php.net/get/oci8-1.4.10.tgz
解压:tar -zxvf oci8-1.4.10.tgz
安装:
cd oci8-1.4.10/usr/local/php/bin/phpize CFLAGS="-I/usr/lib/oracle/11.2.0.4.0/client64/" CXXFLAGS="-I/usr/lib/oracle/11.2.0.4.0/client64/"
./configure --with-php-config=/usr/local/php/bin/php-config --with-oci8=instantclient,/usr/lib/oracle/11.2.0.4.0/client64/lib/
make
make instal
lphp路径和oracle client的路径需根据实际情况进行修改。
如果系统中存在pecl程序,可通过以下命令来安装oci8扩展:
pecl install oci8
系统环境与php配置修改修改系统配置:
echo "/usr/lib/oracle/11.2.0.4.0/client64//lib/"
>/etc/ld.so.conf.d/oracle_client.conf/sbin/ldconfig
修改php.ini配置文件:
extension_dir = "/usr/local/php5/lib/ext"
extension = "oci8.so"
oci8.privileged_connect = on
重启apache使修改生效
/usr/local/apache2/bin/apachectl restart
测试链接代码,确认配置成功
<?php
$conn = oci_connect('qunyingliu', 'testqunyingliu', 'oracle.test.localhost/orcl');
//需要根据实际配置信息进行修改
$stid = oci_parse($conn, 'select table_name from user_tables');
//数据库操作语句可根据实际需求进行修改
oci_execute($stid);
echo "<table>\n";
while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
echo "
\n";
foreach ($row as $item) {
echo "<td>".($item !== null ? htmlentities($item, ENT_QUOTES) : " ")."\n";
}
echo "\n";
}
echo "</table>\n";
?>
相关参考:
http://php.net/manual/zh/function.oci-connect.php
http://php.net/manual/zh/book.oci8.php
http://pecl.php.net/get/oci8-1.4.10.tgz
http://pecl.php.net/package-changelog.php?package=oci8
http://www.oracle.com/technetwork/cn/topics/linuxx86-64soft-092277.html