PHP连接Oracle数据库

  • 基础环境
  1. CentOS 7.2.1511
  2. PHP Version 5.6.31
  3. Oracle-xe-11g
  • 扩展选择
  1. Oracle OCI8
  • 安装依赖
  1. 下载rpm包
    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
  2. 安装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
  • 查看安装结果
[root@d13306817df8 /]$ rpm -qa | grep oracle
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64
oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64
  • 配置instantclient
[root@d13306817df8 /]$ echo '/usr/lib/oracle/11.2/client64/lib/' > /etc/ld.so.conf.d/oracle-x86_64.conf
[root@d13306817df8 /]$ ln -s /usr/lib/oracle/11.2/client64 /usr/lib/oracle/11.2/client  
[root@d13306817df8 /]$ ln -s /usr/include/oracle/11.2/client64 /usr/include/oracle/11.2/client
// vim /etc/profile && . /etc/profile
[root@d13306817df8 /]$ export ORACLE_HOME=/usr/lib/oracle/11.2/client64/  
[root@d13306817df8 /]$ export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64:$LD_LIBRARY_PATH  
[root@d13306817df8 /]$ export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
  • 安装扩展
[root@d13306817df8 /]$ /usr/local/php56/bin/phpize 
[root@d13306817df8 /]$ cd /root/php-5.6.31/ext/oci8/
[root@d13306817df8 /]$ ./configure --with-php-config=/usr/local/php56/bin/php-config --with-oci8=shared,instantclient,/usr/lib/oracle/11.2/client64/lib
[root@d13306817df8 /]$ make
[root@d13306817df8 /]$ make install
[root@d13306817df8 /]$ cd /usr/local/php56/lib/php/extensions/no-debug-non-zts-20131226/
[root@d13306817df8 /]$ ls 
ZendGuardLoader.so  memcache.so  oci8.so  opcache.a  opcache.so  opcache_zend.so  swoole_loader56.so
[root@d13306817df8 /]$  echo 'extension = oci8.so' > /usr/local/php56/etc/php.d/oci8.ini
  • 查看安装结果
[root@d13306817df8 /]$ php --ri oci8

oci8

OCI8 Support => enabled
OCI8 DTrace Support => disabled
OCI8 Version => 2.0.12
Revision => $Id: 020312b6429ebb9d6272ac9bc28f6dce529434b6 $
Oracle Run-time Client Library Version => 11.2.0.4.0
Oracle Compile-time Instant Client Version => 11.2

Directive => Local Value => Master Value
oci8.max_persistent => -1 => -1
oci8.persistent_timeout => -1 => -1
oci8.ping_interval => 60 => 60
oci8.privileged_connect => Off => Off
oci8.statement_cache_size => 20 => 20
oci8.default_prefetch => 100 => 100
oci8.old_oci_close_semantics => Off => Off
oci8.connection_class => no value => no value
oci8.events => Off => Off

Statistics =>  
Active Persistent Connections => 0
Active Connections => 0
  • 代码使用实例
  • 补充说明
    1. Oracle的安装其实是比较烦的,所以我选择使用了Docker
      docker pull alexeiled/docker-oracle-xe-11g
    2. Oracle的表名是大写的,刚才创建的表应该是MASON
      select * from user_tables WHERE TABLE_NAME = 'MASON'

--end--

你可能感兴趣的:(PHP连接Oracle数据库)