Oracle With PHP Connection

Hi guys, I want to share my experience, and share a bit about Oracle and PHP databases … what’s the relationship between the two ?? Most of these questions will definitely be asked by beginner PHP programmers … mmmm but that doesn’t mean I’m adept !! :), because I also knew PHP from the beginning when I only knew the PHP database, that was … the MySQL really took a very long time, and finally my custom made the application using PHP, the database is definitely not far from MySQL (although I already know many databases other than MySQL can collaborate with PHP: \), until finally I was forced to deal with databases that I never thought … was that ???? yup I apparently had to deal with commercial databases (directly 2 more seeds) namely SQL Server and Oracle collaborated with PHP. How come ??? i was at first lost, how do you connect both with PHP. As a result google plays again accompanied by reading the manual from its own PHP, for SQL Server I did not find difficulties coz the connection is not much different from MySQL plus the syntax is almost similar (so I don’t tell man): p but with oracle … MY GOD, THAT’s so Hard; o Man !!!

First, I will tell you about the architecture of the application that I will make:

SQL Server is on a database server with IP 172.17.50.XX

Web Server with Apache and PHP with IP 172.17.50.XX

Oracle is on a database server with IP 192.168.0.XXX and 192.168.0.XX

Everything is connected to the gateway (sorry IP can’t be mentioned).

So … the conclusion is that all databases are not on one machine (ask why ??? how do I know it’s not what I made … but it seems to be related to security).

Then … we continue to my curhatan, until where? oh well until MY GOD, THAT’s so Hard; o Man !!!, Why ??? very rarely tutorials about PHP with Oracle plus manual PHP also hasn’t helped much (the problem is I am very blind).

Ok … I add the specs from the Oracle Server itself using Oracle 9i and 8i and the Oracle Server 7.3.3 web server computer is installed, the function of the Oracle Client is as a link to the Oracle server … this is where the main TrouBLE is, first I try follow the PHP manual how to activate the Oracle module to be supported by PHP:

- // Open “;” in (php.ini)

; extension = php_oci8.dll

; extension = php_oracle.dll

The explanation is easy man … but apparently it’s not that easy !! Until the peach pale I still does not work on the module, with a warning that oci.dll was not found on our OS (even though there is one), I was idly googling and looking for the oci.dll file and found it finally finally accompanied by the manual:

This dll-file was downloaded from [url=][/ url] Install notes:

Use ExpressZIP to extract the file to your windows\system directory

n walah successful too … when I copy oci.dll on System32 and System, PHP recognizes the oci8 and oracle modules;) … fix the problem ?? not yet man, cos after I try to enter this link script like this “


$c1 = OCILogon(“$username”,”$password”,”$dbname” ); if ($c1 == false){ echo OCIError($connection).”not connected”; exit;

} else


echo “<b>success connecting to my oracle databases”;



The order I get is always Warning:

_oci_open_server: ORA-12154:

TNS:could not resolve service name

I want to googling again the meaning of the error message … apparently there must be a tnsnames.ora file in the oracle folder that contains a configuration that directs to the location and name of the service of the Oracle server:

— // catt: semua konfigurasi dah gw edit



(ADDRESS = (PROTOCOL = TCP)(Host = hostname-or-ipaddress)(Port = 1521))


(SID = sidname)

) )

hmmm … the file is already in the correct configuration !!! : \, then where is the problem ??? I reread the PHP manual, finally … I didn’t see a statement like this:

“These functions allow you to access Oracle8 and Oracle7 databases. It uses the Oracle8 Call-Interface (OCI8). You will need the Oracle8 client libraries to use this extension.”

My..GOD that it’s, Oracle8 client libraries to use this extension while I use Oracle7.3.3 clients that have different configurations with Oracle8i and 9i which do not recognize the SID parameters and have replaced them with the SERVICE_NAME parameter; o, yeah I finally replaced the client with The Oracle9i client and reconfiguring its tnsnames.ora file looks like this:




(ADDRESS = (PROTOCOL = TCP)(HOST = hostname-or-ipaddress)(PORT =




(SERVICE_NAME = servicename)



Finish …. ??? huuuuuuaaaaaaaaa: o cry, I can’t, man too !!! I am almost desperate to break up, where’s the under presure anyway: \, tinkering again, I … checked php manually until I entered an external forum and entered a mailing list … the result was I found the answer, because the Oracle server was on another network then we must first describe the SERVICE_NAME in our PHP script, confused ??? yeah i’ve just given an example right away … let’s just edit the connection.php file


$db = “ (DESCRIPTION =

(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.XXX)(PORT = 1521))

(CONNECT_DATA = (SERVICE_NAME = servicename))

)”; $c1 = OCILogon(“username”,”password”,$db);

if ($c1 == false){ echo OCIError($connection).”not connected”; exit;

} else


echo “<b>success connecting to my oracle databases”;



Taaaadaaaaaa…..berhasil man, now it will work.

Ok … that’s the first story of my struggle, for the masters sorry if I feel really stupid … but I really am stupid about this problem: p, then I feel like I’m going to break it for a while … when I finished it took more than a week to just spend connect PHP with Oracle Server: \. Then order me, keep on experimenting … if you find a problem, find out for yourself first (googling, forums, mailing lists, etc.), I’ve been stuck really, then just asking for help immediately.

Lastly … for PHP programmers, don’t get bogged down with MySQL !!! cos enterprise uses Oracle and SQL Server a lot. OK … Goodluck for Us, see you next time !!! :)

Author: roninmorgue

Date: Aug, 23th 2006

Location: Indonesia, Jakarta

Copyright / License:

All X-Code Magazine material can be downloaded, read, modified and distributed freely for non-commercial purposes, provided that it does not delete or change the author’s attributes.

Copyright in the hands of the author and X-Code Magazine by following the license GPL (General Public License).

Bundet is a reflection of failure in managing ideas and mixing them to put an end to brain noise — we’re here