|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
会有很多高手的鼓励,新手的支持,慢慢你劲头就十足,有更多的信心和兴趣去学。oracle|数据|数据库 经由过程PHP你可以轻松的毗连到数据库,恳求数据并将其显示在你的web站点中,乃至修正数据库中的数据。MySQL是一种很盛行的数据库,而且在互联网中有很多有关PHP与MySQL的教程。MySQL是收费的,这一点或许就吸引了很多人。因为其普遍使用,我就不想在这里赘述MySQL的利用办法了。Oracle被大批在企业使用中采取,因而咱们就使用Oracle来引见PHP与数据库的毗连。咱们固然不会说起Oracle数据库的设计道理,缘由是这已超越了咱们的会商局限。
PHP供应了两套函数与Oracle毗连,分离是ORA_和OCI函数。个中ORA_函数略显陈腐。OCI函数更新听说更好一些。二者的利用语法几近相差无几。你的PHP装置选项应当可以撑持二者的利用。
毗连
< ? if ($conn=Ora_Logon("user@TNSNAME","password"))
{ echo "SUCCESS ! Connected to database ";
}else
{echo "Failed :-( Could not connect to database ";}
Ora_Logoff($conn);
phpinfo();
?>
以上代码利用TNSNAME(在你的tnsnames.ora文件中指明)界说的Oracle数据库称号、用户称号和暗码毗连数据库。在胜利毗连的基本上,ora_logon函数前往一个非零的毗连ID并贮存在变量$conn中。
查询
假定与数据库已毗连停当,上面咱们就来实践的使用对数据库的查询。上面的代码演示了一个毗连并查询的典范例子:
function printoraerr($in_cur)
{
// 反省Oracle是不是失足
// 假如存在毛病则显示
// 当指针被激活时每次恳求Oracle后挪用该函数
if(ora_errorcode($in_cur))
echo "Oracle code - ".ora_error($in_cur)." ";
return;
}
if (!($conn=ora_logon("user@TNSNAME","password")))
{echo "Connection to database failed ";
exit;
}
echo "Connected as connection - $conn ";
echo "Opening cursor ... ";
$cursor=ora_open($conn); printoraerr($cursor);
echo "Opened cursor - $cursor ";
$qry="select user,sysdate from dual";
echo "Parsing the query $qry ... ";
ora_parse($cursor,$qry,0); printoraerr($cursor);
echo "Query parsed ";
echo "Executing cursor ... ";
ora_exec($cursor); printoraerr($cursor);
echo "Executed cursor ";
echo "Fetching cursor ... ";
while(ora_fetch($cursor))
{
$user=ora_getcolumn($cursor,0); printoraerr($cursor);
$sysdate=ora_getcolumn($cursor,1); printoraerr($cursor);
echo " row = $user, $sysdate ";
}
echo "Fetched all records ";
echo "Closing cursor ... ";
ora_close($cursor);
echo "Closed cursor ";
echo "Logging off from oracle... ";
ora_logoff($conn);
echo "Logged off from oracle ";
?>
显示了局
以下代码演示了如何查询数据库并将了局输入:
function printoraerr($in_cur, $conn)
{
// 反省Oracle是不是失足
// 假如存在毛病则显示
// 当指针被激活时每次恳求Oracle后挪用该函数
// If it encountered an error, we exit immediately
if(ora_errorcode($in_cur))
{echo "Oracle code - ".ora_error($in_cur)." n";
ora_logoff($conn);
exit;
}
return;
}
function exequery($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 " ";
for ($i=0;$i<$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
echo " ".ora_columnname($cursor,$i)." ";
}
echo " ";
while(ora_fetch($cursor))
{
echo " ";
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)." ";
else
echo " ".ora_getcolumn($cursor,$i)." ";
printoraerr($cursor,$conn);
}
$numrows++;
echo " ";
}
if ($numrows==0)
echo " Query returned no records ";
else
{
echo " ";
echo " Count ";
echo " $numrows ";
echo " ";
}
echo " ";
ora_close($cursor);
return;
}
// 主法式
if(!($conn=ora_logon("user@SID","password")))
{
echo "Error: Cannot connect to database ";
exit;
}
$qry="SELECT
deptno "Dept"
,empno "Emp"
,empnm "Name"
,salary "Salary"
FROM
employee
ORDER BY 1,2";
exequery($qry);
ora_logoff($conn);
?>
基于HTTP的Oracle登录
将以下代码加在PHP页面代码之前以确认Oracle登录。注重你必需准确设定$ SID。
if(!isset($PHP_AUTH_USER))
{
Header("WWW-authenticate: basic realm="$SID"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
echo "
You are not authorized to enter the site
";
exit;
}
else
{
if (!($conn=ora_logon("$PHP_AUTH_USER@$SID",$PHP_AUTH_PW)))
{Header("WWW-authenticate: basic realm="$SID"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
echo "
You are not authorised to enter the site
";
exit;
}}
?>
然后大吼:别人可以,我为什么就不可以?(是不是有点阎罗教练的味道,默默的确是电影看多了,抽嘴巴是会痛的,各位其实明白这个道理了就行了) |
|