Shanghai WTO Forum

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4157|回复: 0

PHP新手上路(十二)

[复制链接]
发表于 2007-12-3 17:53:37 | 显示全部楼层 |阅读模式
使用PHP来操作Oracle数据库 <></P><>11. 数据库连接 </P><>  在上一节里,我们已经介绍了PHP与MySQL数据库的一些基本操作知识,在互联网中有关PHP与MySQL的教程也最多。MySQL是免费的,这一点也许就吸引了不少人。由于其广泛应用,我就不想在这里赘述MySQL的使用方法了。Oracle被大量在企业应用中采用,因此我们就利用Oracle来进一步介绍PHP与数据库的连接。我们当然不会提及Oracle数据库的设计原理,原因是这已经超出了我们的讨论范围。 </P><>  PHP提供了两套函数与Oracle连接,分别是ORA_和OCI函数。其中ORA_函数略显陈旧。OCI函数更新据说更好一些。两者的使用语法几乎相差无几。如前所述,你的PHP安装选项应该可以支持两者的使用。 <BR>  想获得更多有关在Microsoft Windows平台上安装支持PHP3的Apache服务器的知识以及更多有关Oracle数据库的知识,请查阅以下URL:www.csoft.net/~vsbabu/articles/oraphp.html。 </P><>11.1 连接 </P><><? <BR>if ($conn=Ora_Logon("user@TNSNAME","password")) <BR>{ <BR>echo "<B>SUCCESS ! Connected to database<B>n"; <BR>} <BR>else <BR>{ <BR>echo "<B>Failed :-( Could not connect to database<B>n"; <BR>} <BR>Ora_Logoff($conn); <BR>phpinfo(); <BR>?> <BR>以上代码使用TNSNAME(在你的tnsnames.ora文件中指明)定义的Oracle数据库名称、用户名称和密码连接数据库。在成功连接的基础上,ora_logon函数返回一个非零的连接ID并储存在变量$conn中。 </P><>11.2 查询 </P><>假设与数据库已经连接就绪,下面我们就来实际的应用对数据库的查询。下面的代码演示了一个连接并查询的典型例子: <BR><? <BR>/* <BR>* 连接数据库并执行查询 <BR>*/ <BR>function printoraerr($in_cur) <BR>{ <BR>// 检查Oracle是否出错 <BR>// 如果存在错误则显示 <BR>// 当指针被激活时每次请求Oracle后调用该函数 <BR>if(ora_errorcode($in_cur)) <BR>echo "Oracle code - ".ora_error($in_cur)."n"; <BR>return; <BR>} <BR>/** 主程序 */ <BR>if (!($conn=ora_logon("user@TNSNAME","password"))) <BR>{ <BR>echo "Connection to database failedn"; <BR>exit; <BR>} <BR>echo "Connected as connection - <b>$conn</b><br>n"; <BR>echo "Opening cursor ...<br>n"; <BR>$cursor=ora_open($conn); printoraerr($cursor); <BR>echo "Opened cursor - <b>$cursor</b><br>n"; <BR>$qry="select user,sysdate from dual"; <BR>echo "arsing the query <b>$qry</b> ...<br>n"; <BR>ora_parse($cursor,$qry,0); printoraerr($cursor); <BR>echo "Query parsed <br>n"; <BR>echo "Executing cursor ...<br>n"; <BR>ora_exec($cursor); printoraerr($cursor); <BR>echo "Executed cursor<br>n"; <BR>echo "Fetching cursor ...<br>n"; <BR>while(ora_fetch($cursor)) <BR>{ <BR>$user=ora_getcolumn($cursor,0); printoraerr($cursor); <BR>$sysdate=ora_getcolumn($cursor,1); printoraerr($cursor); <BR>echo " row = <B>$user, $sysdate </B><br>n"; <BR>} <BR>echo "Fetched all records<br>n"; <BR>echo "Closing cursor ...<br>n"; <BR>ora_close($cursor); <BR>echo "Closed cursor<br>n"; <BR>echo "Logging off from oracle... <br>n"; <BR>ora_logoff($conn); <BR>echo "Logged off from oracle <br>n"; <BR>?> <BR>(译者注:以上代码段缺少注释,请读者参考PHP Manual的Oracle数据库函数部分) </P><>11.3 显示结果 </P><P>以下代码演示了怎样查询数据库并将结果输出: <BR><? <BR>function printoraerr($in_cur, $conn) <BR>{ <BR>// 检查Oracle是否出错 <BR>// 如果存在错误则显示 <BR>// 当指针被激活时每次请求Oracle后调用该函数 <BR>// If it encountered an error, we exit immediately <BR>if(ora_errorcode($in_cur)) <BR>{ <BR>echo "Oracle code - ".ora_error($in_cur)."<br>n"; <BR>ora_logoff($conn); <BR>exit; <BR>} <BR>return; <BR>} </P><P>function exequery($w_qry,$conn) <BR>{ <BR>$cursor=ora_open($conn); printoraerr($cursor,$conn); <BR>ora_parse($cursor,$w_qry,0); printoraerr($cursor,$conn); <BR>ora_exec($cursor); printoraerr($cursor,$conn); <BR>$numrows=0; <BR>$w_numcols=ora_numcols($cursor); <BR>// 显示头部 <BR>echo " <BR><TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="2"> <BR><TR>n"; <BR>for ($i=0;$i<$w_numcols;$i++) <BR>{ <BR>$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT"; <BR>echo "t<TH VALIGN=TOP ALIGN=$align>".ora_columnname($cursor,$i)."</TH>n"; <BR>} <BR>echo "</TR>n"; <BR>while(ora_fetch($cursor)) <BR>{ <BR>echo "<TR>n"; <BR>for ($i=0;$i<$w_numcols;$i++) <BR>{ <BR>$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT"; <BR>if(ora_columntype($cursor,$i)=="LONG") <BR>echo "<TD VALIGN=TOP ALIGN=$align><PRE>". <BR>ora_getcolumn($cursor,$i)."</PRE></TD>n"; <BR>else <BR>echo "<TD VALIGN=TOP ALIGN=$align>".ora_getcolumn($cursor,$i)."</TD>n"; <BR>printoraerr($cursor,$conn); <BR>} <BR>$numrows++; <BR>echo "</TR>n"; <BR>} <BR>if ($numrows==0) <BR>echo "<TR><TD COLSPAN="$w_numcols"><B>Query returned no records <BR></B></TD></TR>n"; <BR>else <BR>{ <BR>echo "<TR>n"; <BR>echo "<TH COLSPAN="".($w_numcols-1)."" ALIGN=RIGHT>Count</TH>n"; <BR>echo "<TH ALIGN=RIGHT>$numrows</TH>n"; <BR>echo "</TR>n"; <BR>} <BR>echo "</TABLE>n"; <BR>ora_close($cursor); <BR>return; <BR>} </P><P>// 主程序 <BR>if(!($conn=ora_logon("user@SID","password"))) <BR>{ <BR>echo "Error: Cannot connect to databasen"; <BR>exit; <BR>} <BR>$qry="SELECT <BR>deptno "Dept" <BR>,empno "Emp" <BR>,empnm "Name" <BR>,salary "Salary" <BR>FROM <BR>employee <BR>ORDER BY 1,2"; <BR>exequery($qry); <BR>ora_logoff($conn); <BR>?> <BR>(译者注:以上代码段缺少注释,请读者参考PHP Manual的Oracle数据库函数部分) </P><P>11.4 基于HTTP的Oracle登录 </P><P>将以下代码加在PHP页面代码之前以确认Oracle登录。注意你必须正确设定$ SID。 <BR><? <BR>if(!isset($PHP_AUTH_USER)) <BR>{ <BR>Header("WWW-authenticate: basic realm="$SID""); <BR>Header("HTTP/1.0 401 Unauthorized"); <BR>$title="Login Instructions"; <BR>echo "<blockquote> <BR>You are not authorized to enter the site <BR></blockquote> n"; <BR>exit; <BR>} <BR>else <BR>{ <BR>if (!($conn=ora_logon("$PHP_AUTH_USER@$SID",$PHP_AUTH_PW))) <BR>{ <BR>Header("WWW-authenticate: basic realm="$SID""); <BR>Header("HTTP/1.0 401 Unauthorized"); <BR>$title="Login Instructions"; <BR>echo "<blockquote> <BR>You are not authorised to enter the site <BR></blockquote> n"; <BR>exit; <BR>} <BR>} <BR>?> </P>
您需要登录后才可以回帖 登录 | 注册

本版积分规则


QQ|Archiver|mobile|The little black house|Shanghai WTO Net ( 沪ICP备10034107号-3 )

GMT+8, 2024-12-22 19:11

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表