windwos下使用php連接oracle數(shù)據(jù)庫的過程分享
要使用php連接oracle,基本條件是
1.需要你安裝了php、
2.安裝了oracle、
3.配置了tnsname.ora。
本地命令行使用sqlplus能夠連接到oracle。
根據(jù)你機(jī)器的版本選對64bit或者32bit的php程序,我們使用php的oci8擴(kuò)展連接oracle
安裝好php后,打開oci8擴(kuò)展,
寫一段連接oracle的ora.php代碼
<?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);
?>
說明:
oci_connect('hr', 'welcome', 'MYDB')
第一個參數(shù)是oracle的用戶名,
第二個參數(shù)是oracle的密碼
第三個參數(shù)是tnsnames.ora里的連接串名
命令行下執(zhí)行
提示如下錯誤
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
開始以為是沒有選對版本,我是64位的機(jī)器,結(jié)果說是win32的程序,一看字面提示,我就重新安裝了新的32bit程序還是報錯。
仔細(xì)查了查發(fā)現(xiàn)在32位像64位遷移的問題,出現(xiàn)如下問題時,我們需要安裝Oracle Instant Client。
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,它是一個解壓后就能使用的程序,不需要安裝。
如果有oracle賬號的可以去oracle下載對應(yīng)的版本,(注冊用戶需要一堆信息)
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
嫌麻煩的同學(xué)使用這個地址下載
http://eduunix.ccut.edu.cn/index2/database/Oracle%20Instant%20Client/
下載后把壓縮包解壓到c:\oracleinstantclient,并添加路徑到環(huán)境變量PATH
重新執(zhí)行php ora.php,“%1 不是有效的 Win32 應(yīng)用程序”的錯誤沒有了,但是會提示
代碼是從php官網(wǎng)直接拷過來的,代碼中有不可見的字符,使用notepad++查看所有字符,去掉亂碼即可。
繼續(xù)執(zhí)行,這次提示,
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沒有找到tnsnames.ora的位置,時間比較趕,那我就直接使用ip的形式,具體格式根據(jù)你的信息拼寫oci_connect的第三個參數(shù)
oracle10格式:[//]host_name[:port][/service_name]
oracle11格式:[//]host_name[:port][/service_name][:server_type][/instance_name].
我具體使用的php oci連接串是:
配好上述信息后,終于能出結(jié)果了,但是發(fā)現(xiàn)查出來的結(jié)果中問亂碼,這種問題基本都是編碼不匹配。
php oci8中文亂碼解決辦法,先查詢你的oracle的數(shù)據(jù)庫編碼使用,
查出來的結(jié)果是SIMPLIFIED CHINESE_CHINA.ZHS16GBK,在php的代碼里設(shè)置環(huán)境變量
終于php能夠正確連接到oracle啦。
- php連接oracle數(shù)據(jù)庫及查詢數(shù)據(jù)的方法
- php連接oracle數(shù)據(jù)庫的方法(測試成功)
- ThinkPHP 連接Oracle數(shù)據(jù)庫的詳細(xì)教程[全]
- Linux下PHP連接Oracle數(shù)據(jù)庫
- Win7 64位系統(tǒng)下PHP連接Oracle數(shù)據(jù)庫
- 用PHP連接Oracle數(shù)據(jù)庫
- ThinkPHP連接Oracle數(shù)據(jù)庫
- php連接oracle數(shù)據(jù)庫的核心步驟
- PHP遠(yuǎn)程連接oracle數(shù)據(jù)庫操作實(shí)現(xiàn)方法圖文詳解
相關(guān)文章
PHP創(chuàng)建PowerPoint2007文檔的方法
這篇文章主要介紹了PHP創(chuàng)建PowerPoint2007文檔的方法,通過PHP第三方插件PHPPowerPoint類庫實(shí)現(xiàn)ppt文件的生成功能,非常具有實(shí)用價值,需要的朋友可以參考下2015-12-12yii2 開發(fā)api接口時優(yōu)雅的處理全局異常的方法
這篇文章主要介紹了yii2 開發(fā)api接口時優(yōu)雅的處理全局異常的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05PHP結(jié)合Jquery和ajax實(shí)現(xiàn)瀑布流特效
php+ajax+jquery實(shí)現(xiàn)無限瀑布流布局 寬度是一定的高度不定的瀑布流布局 也可以說是無縫拼圖 當(dāng)瀏覽器滾動到底部時候自動加載圖片,非常的實(shí)用,需要的小伙伴可以參考下。2016-01-01基于CI(CodeIgniter)框架實(shí)現(xiàn)購物車功能的方法
這篇文章主要介紹了基于CI(CodeIgniter)框架實(shí)現(xiàn)購物車功能的方法,結(jié)合實(shí)例形式分析了CodeIgniter框架購物車功能類的定義及數(shù)據(jù)庫建立相關(guān)sql命令,需要的朋友可以參考下2018-04-04