這篇文章主要講解了“怎么用PHP連接Oracle數(shù)據(jù)庫”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用PHP連接Oracle數(shù)據(jù)庫”吧!
在甘南等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作 網(wǎng)站設(shè)計制作按需制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷,成都外貿(mào)網(wǎng)站建設(shè),甘南網(wǎng)站建設(shè)費用合理。連接
< ?if($conn=Ora_Logon("user@TNSNAME","password")) {echo"SUCCESS!Connectedtodatabase\n"; }else {echo"Failed:-(Couldnotconnecttodatabase\n";} Ora_Logoff($conn); phpinfo(); ?>
以上代碼使用TNSNAME(在你的tnsnames.ora文件中指明)定義的Oracle數(shù)據(jù)庫名稱、用戶名稱和密碼連接數(shù)據(jù)庫。在成功連接的基礎(chǔ)上,ora_logon函數(shù)返回一個非零的連接ID并儲存在變量$conn中。
查詢
假設(shè)與數(shù)據(jù)庫已經(jīng)連接就緒,下面我們就來實際的應(yīng)用對數(shù)據(jù)庫的查詢。下面的代碼演示了一個連接并查詢的典型例子:
functionprintoraerr($in_cur)
{
//檢查Oracle是否出錯
//如果存在錯誤則顯示
//當(dāng)指針被激活時每次請求Oracle后調(diào)用該函數(shù)
if(ora_errorcode($in_cur))
echo"Oraclecode-".ora_error($in_cur)."\n";
return;
}
if(!($conn=ora_logon("user@TNSNAME","password")))
{echo"Connectiontodatabasefailed\n";
exit;
}
echo"Connectedasconnection-$conn\n";
echo"Openingcursor...\n";
$cursor=ora_open($conn);printoraerr($cursor);
echo"Openedcursor-$cursor\n";
$qry="selectuser,sysdatefromdual";
echo"Parsingthequery$qry...\n";
ora_parse($cursor,$qry,0);printoraerr($cursor);
echo"Queryparsed\n";
echo"Executingcursor...\n";
ora_exec($cursor);printoraerr($cursor);
echo"Executedcursor\n";
echo"Fetchingcursor...\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"Fetchedallrecords\n";
echo"Closingcursor...\n";
ora_close($cursor);
echo"Closedcursor\n";
echo"Loggingofffromoracle...\n";
ora_logoff($conn);
echo"Loggedofffromoracle\n";
?>
怎樣用PHP連接Oracle數(shù)據(jù)庫
顯示結(jié)果
以下代碼演示了怎樣查詢數(shù)據(jù)庫并將結(jié)果輸出:
functionprintoraerr($in_cur,$conn)
{
//檢查Oracle是否出錯
//如果存在錯誤則顯示
//當(dāng)指針被激活時每次請求Oracle后調(diào)用該函數(shù)
//Ifitencounteredanerror,weexitimmediately
if(ora_errorcode($in_cur))
{echo"Oraclecode-".ora_error($in_cur)."n";
ora_logoff($conn);
exit;
}
return;
}
functionexequery($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";" .="" else="" numrows="=0)" conn="ora_logon("user@SID","password")))" qry="SELECT deptno\" from="" employee="">
基于HTTP的Oracle登錄
將以下代碼加在PHP頁面代碼之前以確認(rèn)Oracle登錄。注意你必須正確設(shè)定$SID。
if(!isset($PHP_AUTH_USER))
{
Header("WWW-authenticate:basicrealm=\"$SID\"");
Header("HTTP/1.0401Unauthorized");
$title="LoginInstructions";
echo"
Youarenotauthorizedtoenterthesite
\n";
exit;
}
else
{
if(!($conn=ora_logon("$PHP_AUTH_USER@$SID",$PHP_AUTH_PW)))
{Header("WWW-authenticate:basicrealm=\"$SID\"");
Header("HTTP/1.0401Unauthorized");
$title="LoginInstructions";
echo"
Youarenotauthorisedtoenterthesite
\n";
exit;
}}
?>
感謝各位的閱讀,以上就是“怎么用PHP連接Oracle數(shù)據(jù)庫”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么用PHP連接Oracle數(shù)據(jù)庫這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
標(biāo)題名稱:怎么用PHP連接Oracle數(shù)據(jù)庫-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://aaarwkj.com/article26/jcejg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、動態(tài)網(wǎng)站、外貿(mào)建站、網(wǎng)站導(dǎo)航、營銷型網(wǎng)站建設(shè)、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容