php连接访问Oracle是用过oci函数,以下是整理的文档
1.安装Apache和php包
yum install -y httpd php*
2.下载Oracle组件
oracle-instantclient-basic-10.2.0.4-1.i386.rpm
oracle-instantclient-sqlplus-10.2.0.4-1.i386.rpm
oracle-instantclient-devel-10.2.0.4-1.i386.rpm
oracle-instantclient-odbc-10.2.0.4-1.i386.rpm
#rpm -ivh oracle-instantclient*(四个组件全部安装上)
此时会生成/usr/lib/oracle/10.2.0.4/client/lib/目录
3.修改/etc/ld.so.conf文件
#vim /etc/ld.so.conf
追加以下内容
/usr/lib/oracle/10.2.0.4/client/lib/
#ldconfig(执行命令)
4.下载OCI8组件
#tar zxvf oci8-1.4.1.tgz
5.编辑OCI8模块
#cd oci8-1.4.1
#phpize(执行命令)
#./configure --with-oci8=instantclient,/usr/lib/oracle/10.2.0.4/client/lib/
#make install
成功后系统会提示你:oci8.so已经成功放入/usr/lib/php/modules/目录中
6.修改php.ini文件
#vim /etc/php.ini
追加以下内容
extension=oci8.so
7.重启Apache服务
service httpd restart
8.使用phpinfo()函数查看
9.编辑php代码连接测试oracle数据库
<?php
$conn = oci_connect('scott', 'oracle', '192.168.12.133/orcl');
if (!$conn) {
$e = oci_error();
print htmlentities($e['message']);
exit;
}
$query = 'select ename,sal from scott.emp';
$stid = oci_parse($conn, $query);
if (!$stid) {
$e = oci_error($conn);
print htmlentities($e['message']);
exit;
}
$r = oci_execute($stid, OCI_DEFAULT);
if(!$r) {
$e = oci_error($stid);
echo htmlentities($e['message']);
exit;
}
print '<table border="1">';
while($row = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
print '<tr>';
foreach($row as $item) {
print '<td>'.($item?htmlentities($item):' ').'</td>';
}
print '</tr>';
}
print '</table>';
oci_close($conn);
?>
最后通过浏览器浏览页面
如果是远程连接:
附连接代码:
//连接
$dbconn =
oci_connect('username','password',"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.2)(PORT=1521))(CONNECT_DATA=(SID=sid)))");
if ($dbconn) {
echo '连接成功';
}else {
echo '连接失败';
}
//查询
$oci_rs = oci_parse($dbconn,‘SELECT * FROM user_tables'); //预处理SQL
oci_execute($oci_rs); //执行SQL
while ($row = oci_fetch_array($oci_rs,OCI_ASSOC)) {
print_r($row);
}//处理结果