要使用php连接oracle,基本条件是
1.需要你安装了php、
2.安装了oracle、
3.配置了tnsname.ora。
本地命令行使用sqlplus能够连接到oracle。
根据你机器的版本选对64bit或者32bit的php程序,我们使用php的oci8扩展连接oracle
安装好php后,打开oci8扩展,
写一段连接oracle的ora.php代码
代码如下:
<?php
$conn = oci_connect('hr', 'welcome', 'MYDB');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Prepare the statement
$stid = oci_parse($conn, 'SELECT * FROM departments');
if (!$stid) {
$e = oci_error($conn);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Perform the logic of the query
$r = oci_execute($stid);
if (!$r) {
$e = oci_error($stid);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Fetch the results of the query
print "<table border='1'>/n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
print "<tr>/n";
foreach ($row as $item) {
print " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>/n";
}
print "</tr>/n";
}
print "</table>/n";
oci_free_statement($stid);
oci_close($conn);
?>
说明:
oci_connect('hr', 'welcome', 'MYDB')
第一个参数是oracle的用户名,
第二个参数是oracle的密码
第三个参数是tnsnames.ora里的连接串名
命令行下执行
代码如下:php ora.php
提示如下错误
代码如下:
PHP Warning: PHP Startup: Unable to load dynamic library 'C:/php/php_oci8.dll'- %1 不是有效的 Win32 应用程序。 in Unknown on line 0
PHP Parse error: syntax error, unexpected '"user"' (T_CONSTANT_ENCAPSED_STRING) in C:/Users/nginx/Desktop/oraclephpoci/oci.php on line 3
开始以为是没有选对版本,我是64位的机器,结果说是win32的程序,一看字面提示,我就重新安装了新的32bit程序还是报错。
仔细查了查发现在32位像64位迁移的问题,出现如下问题时,我们需要安装Oracle Instant Client。
代码如下:
Unable to load dynamic library 'C:/Program Files (x86)/PHP/ext/php_oci8_11g.dll' - %1 is not a valid Win32 application.
Warning oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that PATH includes the directory with Oracle Instant Client libraries
Oracle Instant Client,它是一个解压后就能使用的程序,不需要安装。
如果有oracle账号的可以去oracle下载对应的版本,(注册用户需要一堆信息)
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
新闻热点
疑难解答