Oracle and PHP

So a few weeks ago i talked about how confusing Oracle and its’ clients can be. While i still think the lack of simple documentation, and confusing download layout are very annoying, I’ve begun to understand how things work. Well.. kind of. And by “kind of”, I totally mean, i’m still pretty lost. At least now i have a dim candle lighting the dark cavern I’m in.

I’m going to go step by step on how I got my WAMP (Windows, Apache, MySQL, PHP) Stack to also incorporate Oracle support. I’m assuming you already have a properly working Apache Webserver running with PHP enabled.

PHP and the Oracle: A layman’s story

1) Uninstall any existing copy of the Oracle Client, unless it’s needed for other functionality. (We had an Oracle 9i Client at work, which was installed on many of the machines, but wasn’t needed in this case, and i couldn’t find any documentation online to get it to work with PHP…)

2) Install the Instant Client provided at the Oracle site. Make sure to choose the proper options, like your architecture, and whatever client you wish; i used ‘Instant Client Package – Basic’ — because it stated it had all the required files for OCI connections, which is what i was using for my Oracle<->Webserver calls.

3) Edit your PATH Environment Variable. Add the directory to where you installed your instant client to the end. For Example, I installed to C:\oracle\instant_client, so i added ‘;C:\oracle\instant_client’ — note the preceding ‘;’, this is needed if the end of your PATH already doesn’t have it. It’s the delimiter for the different directories added to your path.

You only need to add the main directory to which you installed, the php_oci extension will look for the dll’s in that main directory. Also make sure you don’t have the trailing ‘\’ at the end. Depending on your windows installation it might not like that… — Windows can be so picky when it want.. 🙁

4) Open your php.ini file and uncomment the line ‘;extension=php_oci8.dll’ — uncomment means take off the ‘;’ before the ‘extension=’.

5) Create a phpinfo file. Something simple like this will work.

<?php
phpinfo();
?>

Once you’ve created the file and placed it on your webserver, browse to it. Scroll through, if it’s properly set up, you should see a category called “oci8”.  If not, make sure that anywhere where PATH is referenced, it shows your updated PATH — with your Instant Client Directory added. If it’s not there you may need to restart your Windows server. I was having issues with my PATH properly updating and was forced to do this.

Setup should be complete now.  Next is testing.

6) Try to connect to your Oracle server. I personally use the OCI. Something like this should work:

<?php
$user = "username";
$pw = "password";
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = your.host.domain)(PORT = 1521)))(CONNECT_DATA=(SID=yourSID)))";
$conn = ocilogon($user,$pw,$db);
?>

You don’t need a TNSNAMES.ORA, but can use one (as far as i know). To use one just create the file in the root of the instant client installation.
If you don’t get a fatal error you should be good to go now. You now have the functionality to connect to Oracle databases. I’d love to hear how others have done this. Maybe there’s a better way to do it than what i did.

Some good resources for PHP and Oracle:
http://www.oracle.com/technology/pub/articles/oracle_php_cookbook/index.html
http://www.orafaq.com/wiki/PHP_FAQ

Published by Dan

I'm a technophile at heart. I love tech and how it can make our lives better. Let's take a little trip into my mind :P

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.