PHP提供了两套函数与Oracle连接,分别是ORA_和OCI函数。其中ORA_函数略显陈旧。OCI函数更新据说更好一些。两者的使用语法几乎相差无几。你的PHP安装选项应该可以支持两者的使用。
连接
{echo"SUCCESS!Connectedtodatabasen";
}else
{echo"Failed:-(Couldnotconnecttodatabasen";}
Ora_Logoff($conn);
phpinfo();
?>
以上代码使用TNSNAME(在你的tnsnames.ora文件中指明)定义的Oracle数据库名称、用户名称和密码连接数据库。在成功连接的基础上,ora_logon函数返回一个非零的连接ID并储存在变量$conn中。
查询
假设与数据库已经连接就绪,下面我们就来实际的应用对数据库的查询。下面的代码演示了一个连接并查询的典型例子:
functionprintoraerr($in_cur)
{
//检查Oracle是否出错
//如果存在错误则显示
//当指针被激活时每次请求Oracle后调用该函数
if(ora_errorcode($in_cur))
echo"Oraclecode-".ora_error($in_cur)."n";
return;
}
if(!($conn=ora_logon("user@TNSNAME","password")))
{echo"Connectiontodatabasefailedn";
exit;
}
echo"Connectedasconnection-$connn";
echo"Openingcursor...n";
$cursor=ora_open($conn);printoraerr($cursor);
echo"Openedcursor-$cursorn";
$qry="selectuser,sysdatefromdual";
echo"Parsingthequery$qry...n";
ora_parse($cursor,$qry,0);printoraerr($cursor);
echo"Queryparsedn";
echo"Executingcursor...n";
ora_exec($cursor);printoraerr($cursor);
echo"Executedcursorn";
echo"Fetchingcursor...n";
while(ora_fetch($cursor))
{
$user=ora_getcolumn($cursor,0);printoraerr($cursor);
$sysdate=ora_getcolumn($cursor,1);printoraerr($cursor);
echo"row=$user,$sysdaten";
}
echo"Fetchedallrecordsn";
echo"Closingcursor...n";
ora_close($cursor);
echo"Closedcursorn";
echo"Loggingofffromoracle...n";
ora_logoff($conn);
echo"Loggedofffromoraclen";
?>
显示结果
以下代码演示了怎样查询数据库并将结果输出:
functionprintoraerr($in_cur,$conn)
{
//检查Oracle是否出错
//如果存在错误则显示
//当指针被激活时每次请求Oracle后调用该函数
//Ifitencounteredanerror,weexitimmediately
if(ora_errorcode($in_cur))
{echo"Oraclecode-".ora_error($in_cur)."n";
ora_logoff($conn);
exit;
}
return;
}
functionexequery($w_qry,$conn)
{
$cursor=ora_open($conn);printoraerr($cursor,$conn);
ora_parse($cursor,$w_qry,0);printoraerr($cursor,$conn);
ora_exec($cursor);printoraerr($cursor,$conn);
$numrows=0;
$w_numcols=ora_numcols($cursor);
//显示头部
echo"n";
for($i=0;$i<$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
echo"t".ora_columnname($cursor,$i)."n";
}
echo"n";
while(ora_fetch($cursor))
{
echo"n";
for($i=0;$i<$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
if(ora_columntype($cursor,$i)=="LONG")
echo"".
ora_getcolumn($cursor,$i)."n";
else
echo"".ora_getcolumn($cursor,$i)."n";
printoraerr($cursor,$conn);
}
$numrows++;
echo"n";
}
if($numrows==0)
echo"Queryreturnednorecordsn";
else
{
echo"n";
echo"Countn";
echo"$numrowsn";
echo"n";
}
echo"n";
ora_close($cursor);
return;
}
//主程序
if(!($conn=ora_logon("user@SID","password")))
{
echo"Error:Cannotconnecttodatabasen";
exit;
}
$qry="SELECT
deptno"Dept"
,empno"Emp"
,empnm"Name"
,salary"Salary"
FROM
employee
ORDERBY1,2";
exequery($qry);
ora_logoff($conn);
?>
基于HTTP的Oracle登录
将以下代码加在PHP页面代码之前以确认Oracle登录。注意你必须正确设定$SID。
if(!isset($PHP_AUTH_USER))
{
Header("WWW-authenticate:basicrealm="$SID"");
Header("HTTP/1.0401Unauthorized");
$title="LoginInstructions";
echo"
Youarenotauthorizedtoenterthesite
n";
exit;
}
else
{
if(!($conn=ora_logon("$PHP_AUTH_USER@$SID",$PHP_AUTH_PW)))
{Header("WWW-authenticate:basicrealm="$SID"");
Header("HTTP/1.0401Unauthorized");
$title="LoginInstructions";
echo"
Youarenotauthorisedtoenterthesite
n";
exit;
}}
?>