windwos下使用php串連oracle資料庫的過程分享_php執行個體

來源:互聯網
上載者:User

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

根據你機器的版本選對64bit或者32bit的php程式,我們使用php的oci8擴充串連oracle

安裝好php後,開啟oci8擴充,

寫一段串連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')
第一個參數是oracle的使用者名稱,
第二個參數是oracle的密碼
第三個參數是tnsnames.ora裡的串連串名

命令列下執行

複製代碼 代碼如下:
php ora.php

提示如下錯誤

複製代碼 代碼如下:

PHP Warning: PHP Startup: Unable to load dynamic library 'C:\php\php_oci8.dll'- %1 不是有效 Win32 應用程式。 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位的機器,結果說是win32的程式,一看字面提示,我就重新安裝了新的32bit程式還是報錯。

仔細查了查發現在32位像64位遷移的問題,出現如下問題時,我們需要安裝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下載對應的版本,(註冊使用者需要一堆資訊)

http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

嫌麻煩的同學使用這個地址下載

http://eduunix.ccut.edu.cn/index2/database/Oracle%20Instant%20Client/

下載後把壓縮包解壓到c:\oracleinstantclient,並添加路徑到環境變數PATH

重新執行php ora.php,“%1 不是有效 Win32 應用程式”的錯誤沒有了,但是會提示

複製代碼 代碼如下:
syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

代碼是從php官網直接拷過來的,代碼中有不可見的字元,使用notepad++查看所有字元,去掉亂碼即可。

繼續執行,這次提示,

複製代碼 代碼如下:

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的形式,具體格式根據你的資訊拼字oci_connect的第三個參數
oracle10格式:[//]host_name[:port][/service_name]
oracle11格式:[//]host_name[:port][/service_name][:server_type][/instance_name].
我具體使用的php oci串連串是:
複製代碼 代碼如下:
$conn = oci_connect('hr', 'welcome', '//www.jb51.net:1523/sycx');

配好上述資訊後,終於能出結果了,但是發現查出來的結果中問亂碼,這種問題基本都是編碼不匹配。

php oci8中文亂碼解決辦法,先查詢你的oracle的資料庫編碼使用,

複製代碼 代碼如下:
select userenv('language') from dual;

查出來的結果是SIMPLIFIED CHINESE_CHINA.ZHS16GBK,在php的代碼裡設定環境變數
複製代碼 代碼如下:
putenv("NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK");

終於php能夠正確串連到oracle啦。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.