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

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

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

要使用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ò)展,

寫(xiě)一段連接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);

?>

說(shuō)明:
oci_connect('hr', 'welcome', 'MYDB')
第一個(gè)參數(shù)是oracle的用戶(hù)名,
第二個(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è)用戶(hù)需要一堆信息)

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ù)你的信息拼寫(xiě)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.fzitv.net:1523/sycx');

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

php oci8中文亂碼解決辦法,先查詢(xún)你的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啦。

相關(guān)文章

最新評(píng)論

武平县| 常宁市| 宜兰县| 靖安县| 武宁县| 永安市| 苏尼特右旗| 梧州市| 鹤峰县| 木兰县| 呼图壁县| 银川市| 辰溪县| 郯城县| 阳城县| 隆德县| 新乐市| 安顺市| 泰兴市| 乌鲁木齐县| 台南县| 屏南县| 临泽县| 阿拉善左旗| 增城市| 友谊县| 浑源县| 竹山县| 潞西市| 上蔡县| 资中县| 阿勒泰市| 库尔勒市| 沁阳市| 亚东县| 江城| 肇州县| 唐海县| 乐安县| 邢台县| 萨嘎县|