Page 1 of 1

Unable to connect to Oracle thru oci_connect() api

Posted: Tue Feb 09, 2010 8:48 am
by purusothaman
Hi All,

I am not able to connect to Remote Oracle database using below code.

Code: Select all

 
<span> Display something - Top </span>
<?php
 if ($c = oci_connect("puru", "purusoth123", "//mysystem.network1.xyz.com:1522/db1")) {
   echo "Successfully connected to Oracle.";
   oci_close($c);
 } 
 else {
   $err = oci_error();
   echo "Oracle Connect Error " . $err['text'];
 }
?>
<span> Display something - Bottom </span>
 
"Display something - Top" is displayed but no other outputs. ie, neither "Oracle Connect Error" nor "Display something - Bottom" is displayed.

PS :
===
1. mysystem.network1.xyz.com is host name of a system in our LAN. I am able to ping the system by this host name.
2. db1 is my database name in oracle.
3. I am able to connect and query this database through Oracle SQL Developer application.

Steps I following to enable oracle access:
===============================
1. Copied php_oci8.dll and/or php_oci8_11g.dll to "D:\Program Files\PHP\ext" (those files I got from http://downloads.php.net/pierre/).
2. Downloaded Oracle Instant Basic client and unzipped to directory "D:\Program Files\Oracle - instantclient-basic-win32-11.1.0.7.0\instantclient_11_1".
3. added "D:\Program Files\Oracle - instantclient-basic-win32-11.1.0.7.0\instantclient_11_1" to environment variable PATH.
4. In php.ini file, added extension=php_oci8.dll under

Code: Select all

section
5. And already extension_dir="D:\Program Files\PHP\ext" is available(ie, already not commented).
6. Then I restarted Apache and tried above php code.

Its not working.

Please let me know where I am doing mistake.

Note : I already tried with odbc_ api calls but failed. 

Regards,
Purusothaman A

Re: Unable to connect to Oracle thru oci_connect() api

Posted: Tue Feb 09, 2010 9:53 am
by AbraCadaver

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');

Re: Unable to connect to Oracle thru oci_connect() api

Posted: Tue Feb 09, 2010 10:57 pm
by purusothaman
Hi all,

Now I have tried in 2 different approach.

Approach 1 - Using Oracle specific API
=============================

Code: Select all

 $c = oci_connect("puru", "purusoth123", "//mysystem.network1.xyz.com.com:1522/db1");
  
  if ($c) {
   echo "Successfully connected to Oracle.";
   odbc_close($c);
  } 
 
Output was:
=========
Call to undefined function oci_connect() in D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\iseat1\oracle-test.php on line 59
Approach 2 - Using ODBC API
=============================

Code: Select all

 
 
  $c = odbc_connect("Driver={Microsoft ODBC for Oracle};Server=mysystem.network1.xyz.com:1522/db1;Uid=puru;Pwd=purusoth123;", "", "");
  
  if ($c) {
   echo "Successfully connected to Oracle.";
   odbc_close($c);
  }  
 
 
The output was:
============
Warning: odbc_connect(): SQL error: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154: TNS:could not resolve the connect identifier specified, SQL state 08001 in SQLConnect in D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\iseat1\oracle-test.php on line 41
Any clue?

Re: Unable to connect to Oracle thru oci_connect() api

Posted: Wed Feb 10, 2010 6:59 pm
by purusothaman
Still I am not able to use oci_* functions.
But I am able to query thru ODBC api calls.

Code: Select all

I added TNS entry (given below) in tnsnames.ora file and used OraHome92 ODBC driver.
 
MY_DB1 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = mysystem.network1.xyz.com)(PORT = 1522))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = db1)
    )
  )    
 
PHP code used to connect is :
odbc_connect("Driver={Oracle in OraHome92};Dbq=MY_DB1;Uid=puru;Pwd=purusoth;", "puru", "purusoth");
 
 
Since I am new to oracle, I was not aware of tnsnames.ora file.

Thanks everyone.

Regards,
Purusothaman A

Re: Unable to connect to Oracle thru oci_connect() api

Posted: Sun May 02, 2010 10:29 am
by saigopal
hi
see the oracle oci functions in the following url

http://www.phpro.org/manual/function.ora-commitoff.html

see the following code to fetch the data

Code: Select all

<?php
ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log_test_display3.txt'); 
error_reporting(E_ALL);
/* oci_define_by_name example - thies at thieso dot net (980219) */

$conn = oci_connect("scott", "tiger");

$stmt = oci_parse($conn, "SELECT empno, ename FROM emp where ename like 'M%'");

/* the define MUST be done BEFORE oci_execute! */

oci_define_by_name($stmt, "EMPNO", $empno);
oci_define_by_name($stmt, "ENAME", $ename);


oci_execute($stmt);

while (oci_fetch($stmt)) {
    echo "empno:" . $empno . "\n";
    echo "ename:" . $ename . "\n";
echo "<br>";
}

oci_free_statement($stmt);
oci_close($conn);
?>