2009年3月3日 星期二

PHP Connect Oracle DB

請先參考Oracle 的相關設定,確定安裝PHP 的主機可以連接Oracle DB的(或許應該要先安裝Oracle Client)

首先,需將PHP.INI檔作些修改,找到
;extension = php_oci8.dll
;extension = php_oracle.dll

將前端的";"號去除,並將PHP重新啟動

利用phpinfo(); 指令確定是否有看到頁面中有oci8 及oracle 的字眼存在
可建一檔案,檔名為index.php

If PHP is working, you will see "Hello World" below:


echo "Hello world";
phpinfo(); // Print PHP version and config info
?>



PHP 連接 Oracle db

連接DB的方法有兩種
Using the OCI Extension Module -

if ($c=OCILogon("user_name", "password", "DB_Name")) {
echo "Successfully connected to Oracle.n";
OCILogoff($c);
} else {
$err = OCIError();
echo "Oracle Connect Error " . $err[text];
}
?>

Using the ORA Extension Module -

if ($c=ora_logon("user_name@DB_name","password")) {
echo "Successfully connected to Oracle.n";
ora_commitoff($c);
ora_logoff($c);
} else {
echo "Oracle Connect Error " . ora_error();
}
?>

以上的語法可以放在剛剛建立的index.php下


If PHP is working, you will see "Hello World" below:


echo "Hello world";
phpinfo(); // Print PHP version and config info
?>

Using the OCI Extension Module -

if ($c=OCILogon("user_name", "password", "DB_Name")) {
echo "Successfully connected to Oracle.n";
OCILogoff($c);
} else {
$err = OCIError();
echo "Oracle Connect Error " . $err[text];
}
?>

Using the ORA Extension Module -

if ($c=ora_logon("user_name@DB_name","password")) {
echo "Successfully connected to Oracle.n";
ora_commitoff($c);
ora_logoff($c);
} else {
echo "Oracle Connect Error " . ora_error();
}
?>



如果成功,應該要能看見Successfully connected to xxx 或

沒有留言: