适用环境

  • 操作系统:Windows10 64位
  • PHPstudy集成环境

安装过程

安装一,配置文件开启扩展文件

在phpstudy集成工具中打开PHP的配置文件php.ini 。找到 ";extension=php_oci8.dll"这句话,将其前面的分号去掉,结果如下:

1
2
3
extension=php_oci8.dll      ; Use with Oracle 10gR2 Instant Client
extension=php_oci8_11g.dll ; Use with Oracle 11gR2 Instant Client
extension=php_pdo_oci.dll

题外话,一般我们开启扩展支持,只需要在php.ini中打开扩展就可以。
比如要连接oracle数据库,我们就只要打开 php_oci8.dll 和 php_oci8_11g.dll 就可以了。但是我们打开后,查看phpinfo()并没有开启,也没错误提示,这个问题就有点大,经过一番百度后,发现还需要安装客户端,所以这里我们还需要下载oracle的客户端,直接下载只有客户端支持库的。

安装二:下载并安装客户端

下载地址:
32位链接 密码: cp3v
64位链接 密码: wdst

注意:
这里的32位,64位不是指的Windows操作系统,而是要根据你安装的PHP的位数,就好比今天我纠结了好久的为什么不行,然后注意到安装的是64位的客户端,按照系统看的。而PHP却是32位的(我是用的是集成环境 phpStudy),后更换成32位的客户端安装就真的可以了。附:如何判断PHP是32位,还是64位?

配置系统变量

将路径D:\Program Files (x86)\Oracle\Instant Client\bin添加到系统变量中。

配置环境变量.png
配置环境变量.png

根据Oracle服务器的配置,修改Instant Client的配置

Oracle服务器的配置地址:D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora

tnsnames.ora文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# tnsnames.ora Network Configuration File: D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora
# Generated by Oracle configuration tools.

LISTENER_ORCL =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))


ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
(CONNECT_DATA =
(SID = CLRExtProc)
(PRESENTATION = RO)
)
)

ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl.16.22.64)
)
)

参照服务器的配置,修改Instant Client的配置:
Instant Client的地址:D:\Program Files (x86)\Oracle\Instant Client\network\admin\tnsnames.ora
tnsnames.ora文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# tnsnames.ora Network Configuration File.
# Generated by Oracle configuration tools.

# Sample:
#DatabaseName =
# (DESCRIPTION =
# (ADDRESS_LIST =
# (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
# )
# (CONNECT_DATA =
# (SERVER = DEDICATED)
# (SERVICE_NAME = ServiceName)
# (INSTANCE_NAME = InstanceName)
# )
# )

# Modify the following content to fit your own needs.
ORCL=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORCL
)
)

至此,打开phpinfo页面,查看oci8的相关扩展信息是否开启,如图。

php的oci8扩展信息.png
php的oci8扩展信息.png

使用如下代码链接Oracle时报错,报错信息如下

链接代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Create connection to Oracle
$conn = oci_connect("数据库名称", "数据库密码", "//localhost/orcl");
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
// // Close the Oracle connection
oci_close($conn);

错误码:

Oracle 报错ORA-12514: TNS:listener does not currently know of service requested in connec

通过上网查到可通过修改listener.ora文件解决第5步的错误

添加了一段在安装目录D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN 下文件修改了加上一段复制的代码(灰色选中部分):

12514错误配置文件.png
12514错误配置文件.png

1
2
3
4
5
6
7
8
9
10
11
12
13
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
(PROGRAM = extproc)
(ENVS = "EXTPROC_DLLS=ONLY:D:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
)(SID_DESC =
(GLOBAL_DBNAME = ORCL)
(ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
(SID_NAME = ORCL)
)
)

关掉Service和LISTENER,再启动Service和LISTENER

注意: 复制请留意到ORACLE_HOME =目录,复制目录一定是要本机文件目录,细节真的很重要!

参考文档:
在 Linux 和 Windows 上安装 PHP 和 Oracle Instant Client
PHP 无法开启 OCI8 扩展原因
Windows PHP/phpStudy 连接 甲骨文Oracle 数据库 oci8
Oracle 报错ORA-12514: TNS:listener does not currently know of service requested in connec