php用戶端串連Oracle資料庫

來源:互聯網
上載者:User

php串連訪問Oracle是用過oci函數,以下是整理的文檔1.安裝Apache和php包 

 
  1. yum install -y httpd php* 
2.下載Oracle組件oracle-instantclient-basic-10.2.0.4-1.i386.rpmoracle-instantclient-sqlplus-10.2.0.4-1.i386.rpmoracle-instantclient-devel-10.2.0.4-1.i386.rpmoracle-instantclient-odbc-10.2.0.4-1.i386.rpm#rpm -ivh oracle-instantclient*四個組件全部安裝上)此時會產生/usr/lib/oracle/10.2.0.4/client/lib/目錄3.修改/etc/ld.so.conf檔案#vim /etc/ld.so.conf追加以下內容/usr/lib/oracle/10.2.0.4/client/lib/#ldconfig執行命令)4.下載OCI8組件http://pecl.php.net/get/oci8-1.4.1.tgz#tar zxvf oci8-1.4.1.tgz5.編輯OCI8模組#cd oci8-1.4.1#phpize執行命令)#./configure --with-oci8=instantclient,/usr/lib/oracle/10.2.0.4/client/lib/#make install成功後系統會提示你:oci8.so已經成功放入/usr/lib/php/modules/目錄中6.修改php.ini檔案#vim /etc/php.ini追加以下內容extension=oci8.so7.重啟Apache服務service httpd restart8.使用phpinfo()函數查看

650) this.width=650;" border="0" alt="" src="http://www.bkjia.com/uploads/allimg/131229/1913063U7-0.jpg" />

9.編輯php代碼串連測試oracle資料庫

 
  1. <?php 
  2.  
  3. $conn = oci_connect('scott', 'oracle', '192.168.12.133/orcl');  
  4.  
  5. if (!$conn) {  
  6.  
  7. $e = oci_error();  
  8.  
  9. print htmlentities($e['message']);  
  10.  
  11. exit;  
  12.  
  13. }  
  14.  
  15. $query = 'select ename,sal from scott.emp';  
  16.  
  17. $stid = oci_parse($conn, $query);  
  18. if (!$stid) {  
  19.  
  20. $e = oci_error($conn);  
  21.  
  22. print htmlentities($e['message']);  
  23.  
  24. exit;  
  25.  
  26. }  
  27.  
  28. $r = oci_execute($stid, OCI_DEFAULT);  
  29. if(!$r) {  
  30.  
  31. $e = oci_error($stid);  
  32.  
  33. echo htmlentities($e['message']);  
  34.  
  35. exit;  
  36.  
  37. }  
  38.  
  39. print '<table border="1">';  
  40.  
  41. while($row = oci_fetch_array($stid, OCI_RETURN_NULLS)) {  
  42.  
  43. print '<tr>';  
  44.  
  45. foreach($row as $item) {  
  46.  
  47. print '<td>'.($item?htmlentities($item):' ').'</td>';  
  48.  
  49. }  
  50.  
  51. print '</tr>';  
  52.  
  53. }  
  54.  
  55. print '</table>';  
  56.  
  57. oci_close($conn);  
  58.  
  59. ?> 

最後通過瀏覽器瀏覽頁面

650) this.width=650;" border="0" alt="" src="http://www.bkjia.com/uploads/allimg/131229/19130C614-1.jpg" />

本文出自 “老徐的私房菜” 部落格,謝絕轉載!

相關文章

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.