明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

PHP新手上路(12)

[摘要]使用PHP來(lái)操作Oracle數(shù)據(jù)庫(kù) 11. 數(shù)據(jù)庫(kù)連接   在上一節(jié)里,我們已經(jīng)介紹了PHP與MySQL數(shù)據(jù)庫(kù)的一些基本操作知識(shí),在互聯(lián)網(wǎng)中有關(guān)PHP與MySQL的教程也最多。MySQL是免費(fèi)的,這...
使用PHP來(lái)操作Oracle數(shù)據(jù)庫(kù)

11. 數(shù)據(jù)庫(kù)連接

  在上一節(jié)里,我們已經(jīng)介紹了PHP與MySQL數(shù)據(jù)庫(kù)的一些基本操作知識(shí),在互聯(lián)網(wǎng)中有關(guān)PHP與MySQL的教程也最多。MySQL是免費(fèi)的,這一點(diǎn)也許就吸引了不少人。由于其廣泛應(yīng)用,我就不想在這里贅述MySQL的使用方法了。Oracle被大量在企業(yè)應(yīng)用中采用,因此我們就利用Oracle來(lái)進(jìn)一步介紹PHP與數(shù)據(jù)庫(kù)的連接。我們當(dāng)然不會(huì)提及Oracle數(shù)據(jù)庫(kù)的設(shè)計(jì)原理,原因是這已經(jīng)超出了我們的討論范圍。

  PHP提供了兩套函數(shù)與Oracle連接,分別是ORA_和OCI函數(shù)。其中ORA_函數(shù)略顯陳舊。OCI函數(shù)更新?lián)f(shuō)更好一些。兩者的使用語(yǔ)法幾乎相差無(wú)幾。如前所述,你的PHP安裝選項(xiàng)應(yīng)該可以支持兩者的使用。
  想獲得更多有關(guān)在Microsoft Windows平臺(tái)上安裝支持PHP3的Apache服務(wù)器的知識(shí)以及更多有關(guān)Oracle數(shù)據(jù)庫(kù)的知識(shí),請(qǐng)查閱以下URL:www.csoft.net/~vsbabu/articles/oraphp.html。

11.1 連接

<?
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數(shù)據(jù)庫(kù)名稱、用戶名稱和密碼連接數(shù)據(jù)庫(kù)。在成功連接的基礎(chǔ)上,ora_logon函數(shù)返回一個(gè)非零的連接ID并儲(chǔ)存在變量$conn中。

11.2 查詢

假設(shè)與數(shù)據(jù)庫(kù)已經(jīng)連接就緒,下面我們就來(lái)實(shí)際的應(yīng)用對(duì)數(shù)據(jù)庫(kù)的查詢。下面的代碼演示了一個(gè)連接并查詢的典型例子:
<?
/*
* 連接數(shù)據(jù)庫(kù)并執(zhí)行查詢
*/
function printoraerr($in_cur)
{
// 檢查Oracle是否出錯(cuò)
// 如果存在錯(cuò)誤則顯示
// 當(dāng)指針被激活時(shí)每次請(qǐng)求Oracle后調(diào)用該函數(shù)
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 failedn";
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";
?>
(譯者注:以上代碼段缺少注釋,請(qǐng)讀者參考PHP Manual的Oracle數(shù)據(jù)庫(kù)函數(shù)部分)

11.3 顯示結(jié)果

以下代碼演示了怎樣查詢數(shù)據(jù)庫(kù)并將結(jié)果輸出:
<?
function printoraerr($in_cur, $conn)
{
// 檢查Oracle是否出錯(cuò)
// 如果存在錯(cuò)誤則顯示
// 當(dāng)指針被激活時(shí)每次請(qǐng)求Oracle后調(diào)用該函數(shù)
// 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="100%" BORDER="0" CELLSPACING="1" CELLPADDING="2">
<TR>n";
for ($i=0;$i<$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
echo "t<TH VALIGN=TOP ALIGN=$align>".ora_columnname($cursor,$i)."</TH>n";
}
echo "</TR>n";
while(ora_fetch($cursor))
{
echo "<TR>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>n";
else
echo "<TD VALIGN=TOP ALIGN=$align>".ora_getcolumn($cursor,$i)."</TD>n";
printoraerr($cursor,$conn);
}
$numrows++;
echo "</TR>n";
}
if ($numrows==0)
echo "<TR><TD COLSPAN="$w_numcols"><B>Query returned no records
</B></TD></TR>n";
else
{
echo "<TR>n";
echo "<TH COLSPAN="".($w_numcols-1)."" ALIGN=RIGHT>Count</TH>n";
echo "<TH ALIGN=RIGHT>$numrows</TH>n";
echo "</TR>n";
}
echo "</TABLE>n";
ora_close($cursor);
return;
}

// 主程序
if(!($conn=ora_logon("user@SID","password")))
{
echo "Error: Cannot connect to databasen";
exit;
}
$qry="SELECT
deptno "Dept"
,empno "Emp"
,empnm "Name"
,salary "Salary"
FROM
employee
ORDER BY 1,2";
exequery($qry);
ora_logoff($conn);
?>
(譯者注:以上代碼段缺少注釋,請(qǐng)讀者參考PHP Manual的Oracle數(shù)據(jù)庫(kù)函數(shù)部分)

11.4 基于HTTP的Oracle登錄

將以下代碼加在PHP頁(yè)面代碼之前以確認(rèn)Oracle登錄。注意你必須正確設(shè)定$ SID。
<?
if(!isset($PHP_AUTH_USER))
{
Header("WWW-authenticate: basic realm="$SID"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
echo "<blockquote>
You are not authorized to enter the site
</blockquote> 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 "<blockquote>
You are not authorised to enter the site
</blockquote> n";
exit;
}
}
?>

 





標(biāo)簽:PHP新手上路(12)