|
要使用php連接oracle,基本條件是
1.需要你安裝了php、
2.安裝了oracle、
3.配置了tnsname.ora。
本地命令行使用sqlplus能夠連接到oracle。
根據(jù)你機(jī)器的版本選對(duì)64bit或者32bit的php程序,我們使用php的oci8擴(kuò)展連接oracle
安裝好php后,打開(kāi)oci8擴(kuò)展,
寫一段連接oracle的ora.php代碼
復(fù)制代碼 代碼如下:
<?php
$conn = oci_connect('hr', 'welcome', 'MYDB');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Prepare the statement
$stid = oci_parse($conn, 'SELECT * FROM departments');
if (!$stid) {
$e = oci_error($conn);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Perform the logic of the query
$r = oci_execute($stid);
if (!$r) {
$e = oci_error($stid);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Fetch the results of the query
print "<table border='1'>/n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
print "<tr>/n";
foreach ($row as $item) {
print " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>/n";
}
print "</tr>/n";
}
print "</table>/n";
oci_free_statement($stid);
oci_close($conn);
?>
說(shuō)明:
oci_connect('hr', 'welcome', 'MYDB')
第一個(gè)參數(shù)是oracle的用戶名,
第二個(gè)參數(shù)是oracle的密碼
第三個(gè)參數(shù)是tnsnames.ora里的連接串名
命令行下執(zhí)行
復(fù)制代碼 代碼如下:php ora.php
提示如下錯(cuò)誤
復(fù)制代碼 代碼如下:
php Warning: php Startup: Unable to load dynamic library 'C:/php/php_oci8.dll'- %1 不是有效的 Win32 應(yīng)用程序。 in Unknown on line 0
php Parse error: syntax error, unexpected '"user"' (T_CONSTANT_ENCAPSED_STRING) in C:/Users/nginx/Desktop/oraclephpoci/oci.php on line 3
開(kāi)始以為是沒(méi)有選對(duì)版本,我是64位的機(jī)器,結(jié)果說(shuō)是win32的程序,一看字面提示,我就重新安裝了新的32bit程序還是報(bào)錯(cuò)。
仔細(xì)查了查發(fā)現(xiàn)在32位像64位遷移的問(wèn)題,出現(xiàn)如下問(wèn)題時(shí),我們需要安裝Oracle Instant Client。
復(fù)制代碼 代碼如下:
Unable to load dynamic library 'C:/Program Files (x86)/php/ext/php_oci8_11g.dll' - %1 is not a valid Win32 application.
Warning oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that PATH includes the directory with Oracle Instant Client libraries
Oracle Instant Client,它是一個(gè)解壓后就能使用的程序,不需要安裝。
如果有oracle賬號(hào)的可以去oracle下載對(duì)應(yīng)的版本,(注冊(cè)用戶需要一堆信息)
http://www.oracle.com/techNETwork/database/features/instant-client/index-097480.html
嫌麻煩的同學(xué)使用這個(gè)地址下載
http://eduunix.ccut.edu.cn/index2/database/Oracle%20Instant%20Client/
下載后把壓縮包解壓到c:/oracleinstantclient,并添加路徑到環(huán)境變量PATH
重新執(zhí)行php ora.php,“%1 不是有效的 Win32 應(yīng)用程序”的錯(cuò)誤沒(méi)有了,但是會(huì)提示
復(fù)制代碼 代碼如下:syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
代碼是從php官網(wǎng)直接拷過(guò)來(lái)的,代碼中有不可見(jiàn)的字符,使用notepad++查看所有字符,去掉亂碼即可。
繼續(xù)執(zhí)行,這次提示,
復(fù)制代碼 代碼如下:
php Fatal error: ORA-12154: TNS:could not resolve the connect identifier specified in C:/Users/nginx/Desktop/airline/oci.php on line 6
看樣子是php沒(méi)有找到tnsnames.ora的位置,時(shí)間比較趕,那我就直接使用ip的形式,具體格式根據(jù)你的信息拼寫oci_connect的第三個(gè)參數(shù)
oracle10格式:[//]host_name[:port][/service_name]
oracle11格式:[//]host_name[:port][/service_name][:server_type][/instance_name].
我具體使用的php oci連接串是:
復(fù)制代碼 代碼如下:$conn = oci_connect('hr', 'welcome', '//www.jb51.NET:1523/sycx');
配好上述信息后,終于能出結(jié)果了,但是發(fā)現(xiàn)查出來(lái)的結(jié)果中問(wèn)亂碼,這種問(wèn)題基本都是編碼不匹配。
php oci8中文亂碼解決辦法,先查詢你的oracle的數(shù)據(jù)庫(kù)編碼使用,
復(fù)制代碼 代碼如下:select userenv('language') from dual;
查出來(lái)的結(jié)果是SIMPLIFIED CHINESE_CHINA.ZHS16GBK,在php的代碼里設(shè)置環(huán)境變量
復(fù)制代碼 代碼如下:putenv("NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK");
終于php能夠正確連接到oracle啦。
php技術(shù):windwos下使用php連接oracle數(shù)據(jù)庫(kù)的過(guò)程分享,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。