Wednesday, September 17, 2008

Connecting To Oracle 8 With PHP

Connecting to Oracle database with PHP is not as easy as connecting to MySQL. There are few steps to do before your PHP can connect to Oracle database.

In this tutorial, I try to connect to Oracle with PHP from a Windows machine.

The Software
These are the lists of software I used to test this tutorial:
  1. XAMPP 1.6.7
  2. Oracle Instant Client 10.1 for Windows
  3. Oracle 8i Database Server (remote or local)
Preparing The System
These are the steps to preparing your system before you can connect to Oracle using PHP.
  1. Install XAMPP.
  2. Install Oracle if you want to use a local database server.
  3. Extract the Oracle Instant Client to C:
  4. Copy oci.dll file to C:\xampp\apache\bin.
  5. Add the Oracle Instant Client path (C:\instantclient10_1)  to your system PATH.
  6. Add NLS_LANG as a new system variable. Fill it's value with american_america.we8iso8859p1.
  7. Restart your system to make the changes take effect.
Edit Your php.ini
php.ini file for XAMPP can be found in C:\xampp\apache\bin. Open that file with your text editor and find this following line:

;extension=php_oci8.dll

Uncomment that line so it will be looked like this:

extension=php_oci8.dll

Save that file and then restart your Apache web server. If no error message appear that mean the Oracle module for PHP is loaded properly.

Test It!
Now, prepare the PHP script to test the connection with Oracle. I made the script like this:

$host='10.234.2.12';
$user='USER';
$passwd='123456';
$service_name='TEST';

$db='(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)
(HOST='.$host.')(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME = '.$service_name.')))';

$con=oci_connect($user,$passwd,$db);

if($con){
     echo 'Connected to Oracle.';
}
else{
     echo 'Cannot connect to Oracle.';
}

oci_close($con);


Run the script and see if it works.

4 comments:

suyandi said...

Great tutorial :)

sam said...

Ahh Great ..this is wt I wanted

Anonymous said...

Nice Sharing Dear Keep it up....

Unknown said...

I try this but error "Warning: oci_connect() [function.oci-connect]: ORA-03134: Connections to this server version are no longer supported. in D:\xampp\htdocs\test\test.php on line 10
Cannot connect to Oracle.
Warning: oci_close() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\test\test.php on line 19"

do you know this solution?