用php语言实现数据库连接详细代码介绍(2)_PHP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:PHP室宝序竣?php喘送圭塀崙恬抹待夕
凪嶄db_mysql.inc.php,config.php,function.php音頁寔屎聞喘欺議?購囚頁$filename 猟周兆,厘頁宥狛響函方象垂嶄議夕頭兆各 <?php inc

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 "

\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 " Query returned no records

\n";
else
{
echo " \n";
echo " Count \n";
echo " $numrows \n";
echo "
\n";
}
echo " \n";
ora_close($cursor);
return;
}

// 主程序
if(!($conn=ora_logon("user@SID","password")))
{
echo "Error: Cannot connect to database\n";
exit;
}
$qry="SELECT
deptno \"Dept\"
,empno \"Emp\"
,empnm \"Name\"
,salary \"Salary\"
FROM
employee
ORDER BY 1,2";
exequery($qry);
ora_logoff($conn);
?>

(译者注:以上代码段缺少注释,请读者参考PHP Manual的Oracle数据库函数部分)

4 基于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

\n";
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

\n";
exit;
}
}
?>

分享:PHP进阶技巧:如何避免表单的重复提交
我想做一个随机的出现的网页,可是用rand()就是不行,听说要初始化随机数,可是怎么初始化,请不吝赐教,谢谢! //第一步:初始化种子 $seedarray =microtime(); $seedstr =split(" &q

共2页上一页12下一页
来源:模板无忧//所属分类:PHP教程/更新时间:2008-08-22
相关PHP教程