4.據庫連接
通過PHP你可以輕松的連接到數據庫,請求數據並將其顯示在你的web站點中,甚至修改數據庫中的數據。MySQL是一種很流行的數據庫,並且在互聯網中有許多有關PHP與MySQL的教程。MySQL是免費的,這一點也許就吸引了不少人。由於其廣泛應用,我就不想在這裡贅述MySQL的使用方法了。Oracle被大量在企業應用中采用,因此我們就利用Oracle來介紹PHP與數據庫的連接。我們當然不會提及Oracle數據庫的設計原理,原因是這已經超出了我們的討論范圍。
PHP提供了兩套函數與Oracle連接,分別是ORA_和OCI函數。其中ORA_函數略顯陳舊。OCI函數更新據說更好一些。兩者的使用語法幾乎相差無幾。如前所述,你的PHP安裝選項應該可以支持兩者的使用。
想獲得更多有關在Microsoft Windows平台上安裝支持PHP3的Apache服務器的知識以及更多有關Oracle數據庫的知識,請查閱以下URL:www.csoft.net/~vsbabu/articles/oraphp.html。
4.1 連接
<?
if ($conn=Ora_Logon("user@TNSNAME","password"))
{
echo "<B>SUCCESS ! Connected to database<B> ";
}
else
{
echo "<B>Failed :-( Could not connect to database<B> ";
}
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)." ";
return;
}
/** 主程序 */
if (!($conn=ora_logon("user@TNSNAME","password")))
{
echo "Connection to database failed ";
exit;
}
echo "Connected as connection - <b>$conn</b><br> ";
echo "Opening cursor ...<br> ";
$cursor=ora_open($conn); printoraerr($cursor);
echo "Opened cursor - <b>$cursor</b><br> ";
$qry="select user,sysdate from dual";
echo "Parsing the query <b>$qry</b> ...<br> ";
ora_parse($cursor,$qry,0); printoraerr($cursor);
echo "Query parsed <br> ";
echo "Executing cursor ...<br> ";
ora_exec($cursor); printoraerr($cursor);
echo "Executed cursor<br> ";
echo "Fetching cursor ...<br> ";
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> ";
}
echo "Fetched all records<br> ";
echo "Closing cursor ...<br> ";
ora_close($cursor);
echo "Closed cursor<br> ";
echo "Logging off from oracle... <br> ";
ora_logoff($conn);
echo "Logged off from oracle <br> ";
?>
(譯者注:以上代碼段缺少注釋,請讀者參考PHP Manual的Oracle數據庫函數部分)
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="100%" BORDER="0" CELLSPACING="1" CELLPADDING="2">
<TR> ";
for ($i=0;$i<$w_numcols;$i )
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
echo " <TH VALIGN=TOP ALIGN=$align>".ora_columnname($cursor,$i)."</TH> ";
}
echo "</TR> ";
while(ora_fetch($cursor))
{
echo "<TR> ";
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> ";
else
echo "<TD VALIGN=TOP ALIGN=$align>".ora_getcolumn($cursor,$i)."</TD> ";
printoraerr($cursor,$conn);
}
$numrows ;
echo "</TR> ";
}
if ($numrows==0)
echo "<TR><TD COLSPAN="$w_numcols"><B>Query returned no records
</B></TD></TR> ";
else
{
echo "<TR> ";
echo "<TH COLSPAN="".($w_numcols-1)."" ALIGN=RIGHT>Count</TH> ";
echo "<TH ALIGN=RIGHT>$numrows</TH> ";
echo "</TR> ";
}
echo "</TABLE> ";
ora_close($cursor);
return;
}
// 主程序
if(!($conn=ora_logon("user@SID","password")))
{
echo "Error: Cannot connect to database ";
exit;
}
$qry="SELECT
deptno "Dept"
,empno "Emp"