最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

windwos下使用php連接oracle數(shù)據(jù)庫的過程分享

 更新時間:2014年05月26日 11:44:21   作者:  
這篇文章主要介紹了windwos下使用php連接oracle數(shù)據(jù)庫的過程分享,講解了php連接oracle的必要條件、代碼實例以及錯誤排查等,需要的朋友可以參考下

要使用php連接oracle,基本條件是
1.需要你安裝了php、
2.安裝了oracle、
3.配置了tnsname.ora。
本地命令行使用sqlplus能夠連接到oracle。

根據(jù)你機器的版本選對64bit或者32bit的php程序,我們使用php的oci8擴展連接oracle

安裝好php后,打開oci8擴展,

寫一段連接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) : "&nbsp;") . "</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í)行

復(fù)制代碼 代碼如下:
php ora.php

提示如下錯誤

復(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

開始以為是沒有選對版本,我是64位的機器,結(jié)果說是win32的程序,一看字面提示,我就重新安裝了新的32bit程序還是報錯。

仔細查了查發(fā)現(xiàn)在32位像64位遷移的問題,出現(xiàn)如下問題時,我們需要安裝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,它是一個解壓后就能使用的程序,不需要安裝。
如果有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)用程序”的錯誤沒有了,但是會提示

復(fù)制代碼 代碼如下:
syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

代碼是從php官網(wǎng)直接拷過來的,代碼中有不可見的字符,使用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沒有找到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連接串是:
復(fù)制代碼 代碼如下:
$conn = oci_connect('hr', 'welcome', '//m.fzitv.net:1523/sycx');

配好上述信息后,終于能出結(jié)果了,但是發(fā)現(xiàn)查出來的結(jié)果中問亂碼,這種問題基本都是編碼不匹配。

php oci8中文亂碼解決辦法,先查詢你的oracle的數(shù)據(jù)庫編碼使用,

復(fù)制代碼 代碼如下:
select userenv('language') from dual;

查出來的結(jié)果是SIMPLIFIED CHINESE_CHINA.ZHS16GBK,在php的代碼里設(shè)置環(huán)境變量
復(fù)制代碼 代碼如下:
putenv("NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK");

終于php能夠正確連接到oracle啦。

相關(guān)文章

最新評論

安达市| 彩票| 依兰县| 塔河县| 辉县市| 阿鲁科尔沁旗| 金昌市| 上饶县| 兰州市| 颍上县| 萨迦县| 建湖县| 安陆市| 雅安市| 兰州市| 岳阳县| 渭源县| 海原县| 吴桥县| 金坛市| 南平市| 郑州市| 南康市| 宜丰县| 蒙山县| 兖州市| 措美县| 石门县| 霞浦县| 通化市| 花莲市| 陕西省| 泸西县| 南岸区| 聊城市| 怀仁县| 镇雄县| 墨竹工卡县| 莱州市| 霸州市| 临汾市|