你好,欢迎访问远方教程PC版!
广告位招租

php零基础入门第四章:数据编程之显示结果

[日期:2014-11-14]   来源:远方教程  作者:远方教程   阅读:1417次[字体: ] 访问[旧版]
 捐赠远方教程 

  4.3 显示结果

  以下代码演示了怎样查询数据库并将结果输出:

<?
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)."<br>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 "
<TABLE WIDTH=&#92;"100%&#92;" BORDER=&#92;"0&#92;"
CELLSPACING=&#92;"1&#92;" CELLPADDING=&#92;"2&#92;">
<TR>&#92;n";
for ($i=0;$i<$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
echo "&#92;t<TH VALIGN=TOP
ALIGN=$align>".ora_columnname($cursor,$i)."</TH>&#92;n";
}
echo "</TR>&#92;n";
while(ora_fetch($cursor))
{
echo "<TR>&#92;n";
for ($i=0;$i<$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
if(ora_columntype($cursor,$i)=="LONG")
echo "<TD VALIGN=TOP ALIGN=$align><PRE>".
ora_getcolumn($cursor,$i)."</PRE></TD>&#92;n";
else
echo "<TD VALIGN=TOP ALIGN=$align>".ora_getcolumn($cursor,$i)."</TD>&#92;n";
printoraerr($cursor,$conn);
}
$numrows++;
echo "</TR>&#92;n";
}
if ($numrows==0)
echo "<TR><TD COLSPAN=&#92;"$w_numcols&#92;"><B>Query returned no records
</B></TD></TR>&#92;n";
else
{
echo "<TR>&#92;n";
echo "<TH COLSPAN=&#92;"".($w_numcols-1)."&#92;"
ALIGN=RIGHT>Count</TH>&#92;n";
echo "<TH ALIGN=RIGHT>$numrows</TH>&#92;n";
echo "</TR>&#92;n";
}
echo "</TABLE>&#92;n";
ora_close($cursor);
return;
}
// 主程序
if(!($conn=ora_logon("user@SID","password")))
{
echo "Error: Cannot connect to database&#92;n";
exit;
}
$qry="SELECT
deptno &#92;"Dept&#92;"
,empno &#92;"Emp&#92;"
,empnm &#92;"Name&#92;"
,salary &#92;"Salary&#92;"
FROM
employee
ORDER BY 1,2";
exequery($qry);
ora_logoff($conn);
?>
(译者注:以上代码段缺少注释,请读者参考PHP Manual 的Oracle 数据库函数部分)

图片展示
 
相关评论
站长推荐