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

php零基础入门第四章:数据编程之查询记录

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

  本章导读

  假设与数据库已经连接就绪,下面我们就来实际的应用对数据库的查询。下面的代码演示了一个连接并查询的典型例子:

<?
/*
* 连接数据库并执行查询
*/
function printoraerr($in_cur)
{
// 检查Oracle 是否出错
// 如果存在错误则显示
// 当指针被激活时每次请求Oracle 后调用该函数
if(ora_errorcode($in_cur))
echo "Oracle code - ".ora_error($in_cur)."&#92;n";
return;
}
/** 主程序 */
if (!($conn=ora_logon("user@TNSNAME","password")))
{
echo "Connection to database failed&#92;n";
exit;
}
echo "Connected as connection - <b>$conn</b><br>&#92;n";
echo "Opening cursor ...<br>&#92;n";
$cursor=ora_open($conn); printoraerr($cursor);
echo "Opened cursor - <b>$cursor</b><br>&#92;n";
$qry="select user,sysdate from dual";
echo "Parsing the query <b>$qry</b> ...<br>&#92;n";
ora_parse($cursor,$qry,0); printoraerr($cursor);
echo "Query parsed <br>&#92;n";
echo "Executing cursor ...<br>&#92;n";
ora_exec($cursor); printoraerr($cursor);
echo "Executed cursor<br>&#92;n";
echo "Fetching cursor ...<br>&#92;n";
while(ora_fetch($cursor))
{
$user=ora_getcolumn($cursor,0); printoraerr($cursor);
$sysdate=ora_getcolumn($cursor,1); printoraerr($cursor);
echo " row = <B>$user, $sysdate </B><br>&#92;n";
}
echo "Fetched all records<br>&#92;n";
echo "Closing cursor ...<br>&#92;n";
ora_close($cursor);
echo "Closed cursor<br>&#92;n";
echo "Logging off from oracle... <br>&#92;n";
ora_logoff($conn);
echo "Logged off from oracle <br>&#92;n";
?>

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

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