? if ($conn=Ora_Logon( user@TNSNAME , password )) { echo B SUCCESS ! Connected to database B /n } else { echo B Failed :-( Could not connect to database B /n } Ora_Logoff($conn); phpinfo(); ? 以上代码使用TNSNAME(在你的tnsnames.ora文件中指明)定义的Oracle数据库名称、用户名称和密码连接数据库。在成功连接的基础上,ora_logon函数返回一个非零的连接ID并储存在变量$conn中。
4.2 查询
假设与数据库已经连接就绪,下面我们就来实际的应用对数据库的查询。下面的代码演示了一个连接并查询的典型例子: ? /* * 连接数据库并执行查询 */ function printoraerr($in_cur) { // 检查Oracle是否出错 // 如果存在错误则显示 // 当指针被激活时每次请求Oracle后调用该函数 if(ora_errorcode($in_cur)) echo Oracle code - .ora_error($in_cur). /n return; } /** 主程序 */ if (!($conn=ora_logon( user@TNSNAME , password ))) { echo Connection to database failed/n exit; } echo Connected as connection - b $conn /b br /n echo Opening cursor ... br /n $cursor=ora_open($conn); printoraerr($cursor); echo Opened cursor - b $cursor /b br /n $qry= select user,sysdate from dual echo Parsing the query b $qry /b ... br /n ora_parse($cursor,$qry,0); printoraerr($cursor); echo Query parsed br /n echo Executing cursor ... br /n ora_exec($cursor); printoraerr($cursor); echo Executed cursor br /n echo Fetching cursor ... br /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 /n } echo Fetched all records br /n echo Closing cursor ... br /n ora_close($cursor); echo Closed cursor br /n echo Logging off from oracle... br /n ora_logoff($conn); echo Logged off from oracle br /n ? (译者注:以上代码段缺少注释,请读者参考PHP Manual的Oracle数据库函数部分)html教程