我的位置: 首頁 > 學(xué)習(xí)專區(qū) > PHP技術(shù) > php數(shù)據(jù)庫連

php數(shù)據(jù)庫連

2013-04-06 09:51:59
來源:
[導(dǎo)讀] 通過PHP你可以輕松的連接到數(shù)據(jù)庫,請求數(shù)據(jù)并將其顯示在你的web站點中,甚至修改數(shù)據(jù)庫中的數(shù)據(jù)。MySQL是一種很流行的數(shù)據(jù)庫,并且在互聯(lián)...
通過PHP你可以輕松的連接到數(shù)據(jù)庫,請求數(shù)據(jù)并將其顯示在你的web站點中,甚至修改數(shù)據(jù)庫中的數(shù)據(jù)。MySQL是一種很流行的數(shù)據(jù)庫,并且在互聯(lián)網(wǎng)中有許多有關(guān)PHP與MySQL的教程。MySQL是免費的,這一點也許就吸引了不少人。由于其廣泛應(yīng)用,我就不想在這里贅述MySQL的使用方法了。Oracle被大量在企業(yè)應(yīng)用中采用,因此我們就利用Oracle來介紹PHP與數(shù)據(jù)庫的連接。我們當然不會提及Oracle數(shù)據(jù)庫的設(shè)計原理,原因是這已經(jīng)超出了我們的討論范圍。

PHP提供了兩套函數(shù)與Oracle連接,分別是ORA_和OCI函數(shù)。其中ORA_函數(shù)略顯陳舊。OCI函數(shù)更新?lián)f更好一些。兩者的使用語法幾乎相差無幾。如前所述,你的PHP安裝選項應(yīng)該可以支持兩者的使用。

想獲得更多有關(guān)在Microsoft Windows平臺上安裝支持PHP3的Apache服務(wù)器的知識以及更多有關(guān)Oracle數(shù)據(jù)庫的知識,請查閱以下URL:www.csoft.net/~vsbabu/articles/oraphp.html。

4.1 連接

if ($conn=Ora_Logon("user@TNSNAME","password"))

{

echo "SUCCESS ! Connected to database ";

}

else

{

echo "Failed :-( Could not connect to database ";

}

Ora_Logoff($conn);

phpinfo();

?>

以上代碼使用TNSNAME(在你的tnsnames.ora文件中指明)定義的Oracle數(shù)據(jù)庫名稱、用戶名稱和密碼連接數(shù)據(jù)庫。在成功連接的基礎(chǔ)上,ora_logon函數(shù)返回一個非零的連接ID并儲存在變量$conn中。

4.2 查詢

假設(shè)與數(shù)據(jù)庫已經(jīng)連接就緒,下面我們就來實際的應(yīng)用對數(shù)據(jù)庫的查詢。下面的代碼演示了一個連接并查詢的典型例子:

以下是引用片段:

/*

* 連接數(shù)據(jù)庫并執(zhí)行查詢

*/

function printoraerr($in_cur)

{

// 檢查Oracle是否出錯

// 如果存在錯誤則顯示

// 當指針被激活時每次請求Oracle后調(diào)用該函數(shù)

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 - $conn

";

echo "Opening cursor ...

";

$cursor=ora_open($conn); printoraerr($cursor);

echo "Opened cursor - $cursor

";

$qry="select user,sysdate from dual";

echo "Parsing the query $qry ...

";

ora_parse($cursor,$qry,0); printoraerr($cursor);

echo "Query parsed

";

echo "Executing cursor ...

";

ora_exec($cursor); printoraerr($cursor);

echo "Executed cursor

";

echo "Fetching cursor ...

";

while(ora_fetch($cursor))

{

$user=ora_getcolumn($cursor,0); printoraerr($cursor);

$sysdate=ora_getcolumn($cursor,1); printoraerr($cursor);

echo " row = $user, $sysdate

";

}

echo "Fetched all records

";

echo "Closing cursor ...

";

ora_close($cursor);

echo "Closed cursor

";

echo "Logging off from oracle...

";

ora_logoff($conn);

echo "Logged off from oracle

";

?>

(譯者注:以上代碼段缺少注釋,請讀者參考PHP Manual的Oracle數(shù)據(jù)庫函數(shù)部分)

4.3 顯示結(jié)果

以下代碼演示了怎樣查詢數(shù)據(jù)庫并將結(jié)果輸出:

以下是引用片段:

function printoraerr($in_cur, $conn)

{

// 檢查Oracle是否出錯

// 如果存在錯誤則顯示

// 當指針被激活時每次請求Oracle后調(diào)用該函數(shù)

// 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 "

";

for ($i=0;$i<$w_numcols;$i++)

{

$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";

echo " ".ora_columnname($cursor,$i)." ";

}

echo "

";

while(ora_fetch($cursor))

{

echo " ";

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)."

";

else

echo " ".ora_getcolumn($cursor,$i)." ";

printoraerr($cursor,$conn);

}

$numrows++;

echo "

";

}

if ($numrows==0)

echo " Query returned no records

";

else

{

echo " ";

echo " Count ";

echo " $numrows ";

echo "

";

}

echo " ";

ora_close($cursor);

return;

}

// 主程序

if(!($conn=ora_logon("user@SID","password")))

{

echo "Error: Cannot connect to database ";

exit;

}

$qry="SELECT

deptno \"Dept\"

,empno \"Emp\"

,empnm \"Name\"

,salary \"Salary\"

FROM

employee

ORDER BY 1,2";

exequery($qry);

ora_logoff($conn);

?>

(譯者注:以上代碼段缺少注釋,請讀者參考PHP Manual的Oracle數(shù)據(jù)庫函數(shù)部分)

4.4 基于HTTP的Oracle登錄

將以下代碼加在PHP頁面代碼之前以確認Oracle登錄。注意你必須正確設(shè)定$ 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

";

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

";

exit;

}

}

?>

深圳北大青鳥http://www.sbsnmc.com

評論
熱點專題
>>
相關(guān)文章推薦
>>
好吊妞免费视频在线观看,久久亚洲国产人成综合网,久久精品国产2020,欧美精品综合在线
日本精品三级视频 | 一区二区免费高清在线观看国产 | 一本综合九九国产二区 | 中文字幕无线在线视频观 | 免费激情小视频在线观看 | 日韩R级无卡亚洲一区 |