通過PHP你可以輕鬆的串連到資料庫,請求資料並將其顯示在你的web網站中,甚至修改資料庫中的資料。MySQL是一種很流行的資料庫,並且在互連網中有許多有關PHP與MySQL的教程。MySQL是免費的,這一點也許就吸引了不少人。由於其廣泛應用,我就不想在這裡贅述MySQL的使用方法了。Oracle被大量在公司專屬應用程式中採用,因此我們就利用Oracle來介紹PHP與資料庫的串連。我們當然不會提及Oracle資料庫的設計原理,原因是這已經超出了我們的討論範圍。
PHP提供了兩套函數與Oracle串連,分別是ORA_和OCI函數。其中ORA_函數略顯陳舊。OCI函數更新據說更好一些。兩者的使用文法幾乎相差無幾。你的PHP安裝選項應該可以支援兩者的使用。
串連
< ? if ($conn=Ora_Logon("user@TNSNAME","password"))
{ echo "SUCCESS ! Connected to database\n";
}else
{echo "Failed Could not connect to database\n";}
Ora_Logoff($conn);
phpinfo();
?>
以上代碼使用TNSNAME(在你的tnsnames.ora檔案中指明)定義的Oracle資料庫名稱、使用者名稱稱和密碼串連資料庫。在成功串連的基礎上,ora_logon函數返回一個非零的串連ID並儲存在變數$conn中。
查詢
假設與資料庫已經串連就緒,下面我們就來實際的應用對資料庫的查詢。下面的代碼示範了一個串連並查詢的典型例子:
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 - $conn \n";
echo "Opening cursor ... \n";
$cursor=ora_open($conn); printoraerr($cursor);
echo "Opened cursor - $cursor \n";
$qry="select user,sysdate from dual";
echo "Parsing the query $qry ... \n";
ora_parse($cursor,$qry,0); printoraerr($cursor);
echo "Query parsed \n";
echo "Executing cursor ... \n";
ora_exec($cursor); printoraerr($cursor);
echo "Executed cursor \n";
echo "Fetching cursor ... \n";
while(ora_fetch($cursor))
{
$user=ora_getcolumn($cursor,0); printoraerr($cursor);
$sysdate=ora_getcolumn($cursor,1); printoraerr($cursor);
echo " row = $user, $sysdate \n";
}
echo "Fetched all records \n";
echo "Closing cursor ... \n";
ora_close($cursor);
echo "Closed cursor \n";
echo "Logging off from oracle... \n";
ora_logoff($conn);
echo "Logged off from oracle \n";
?>
顯示結果
以下代碼示範了怎樣查詢資料庫並將結果輸出:
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)." 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 " \n";
for ($i=0;$i<$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
echo "\t ".ora_columnname($cursor,$i)." \n";
}
echo " \n";
while(ora_fetch($cursor))
{
echo " \n";
for ($i=0;$i<$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
if(ora_columntype($cursor,$i)=="LONG")
echo " ".
ora_getcolumn($cursor,$i)." \n";
else
echo " ".ora_getcolumn($cursor,$i)." \n";
printoraerr($cursor,$conn);
}
$numrows++;
echo " \n";
}
if ($numrows==0)
echo " Query returned no records \n";
else
{
echo " \n";
echo " Count \n";
echo " $numrows \n";
echo " \n";
}
echo " \n";
ora_close($cursor);
return;
}
// 主程式
if(!($conn=ora_logon("user@SID","password")))
{
echo "Error: Cannot connect to database\n";
exit;
}
$qry="SELECT
deptno \"Dept\"
,empno \"Emp\"
,empnm \"Name\"
,salary \"Salary\"
FROM
employee
ORDER BY 1,2";
exequery($qry);
ora_logoff($conn);
?>
基於HTTP的Oracle登入
將以下代碼加在PHP頁面代碼之前以確認Oracle登入。注意你必須正確設定$ SID。
if(!isset($PHP_AUTH_USER))
{
Header("WWW-authenticate: basic realm=\"$SID\"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
echo "
You are not authorized to enter the site
\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 "
You are not authorised to enter the site
\n";
exit;
}}
?>