ORACLE & PHP CONNECTIVITY
July 30, 2010 Friday

ORACLE & PHP CONNECTIVITY


IMPORTANT LEGAL NOTICE

This tutorial is educational purposes only. I won't accept any responsibility for any damages or losing data by using the information in this page. Use it at your own risc. Otherwise don't use this information. All registered names and rights respected their owners.

PREFACE

If you installed Oracle Database and using normally, You may want to provide web support in your Unix/Linux server. This tutorial will explain how to connect and use Oracle database with Apache Web Server and PHP.

If you are already installed Apache and PHP with Oracle support, but you can not connect to Oracle via PHP, you don't need to read all. So, you may go to "Escaping from OCI() Errors" section.

AUDIENCE

This tutorial is for developers who are writing PHP applications intended to run with Oracle Database under Unix/Linux (especially Sun Solaris) operating systems and System Integrators who want to provide Oracle & PHP support for web developers/users.

CONTENTS

PREREQUISITES

This tutorial has prepared by using the following cook list :))

  • Apache 1.3.27 source tarball
  • PHP 4.3.2 source tarball
  • Oracle 8i (8.1.7) Enterprise Edition. Assuming you installed and running correctly. If not, refer to my "Install Oracle on Solaris" tutorial.
  • Sun Solaris 8/9. If you are not using Solaris 8 or 9, you may make trouble about information in this page.
  • Assuming that you are using gcc-3.3 to compile sources. If not, change "CC=gcc" with a compiler that you prefer to use.
  • Assuming your installed binaries will be located in "/usr/local" directory. If not, change all "/usr/local" directory names to your directory name.

IMPORTANT : If you want to compile Apache and PHP with other modules and properties, please refer to INSTALL files in source folder for each software or visit their web site for additional properties.

GETTING READY

Download Apache 1.3.27 Web Server, PHP 4.3.2 and gcc-3.3 compiler. You can download gcc-3.3 from http://www.sunfreeware.com as a package. If you have got the files, extract it as follows:

#gunzip < apache-1.3.27.tar.gz | tar vxf -
#gunzip < php-4.3.2.tar.gz | tar vxf -

If you have downloaded gcc-3.3 as a package from SunFreeware Web site, unzip and install them as follows firstly:

#gunzip gcc-3.3-sol9-intel-local.gz
#pkgadd -d gcc-3.3-sol9-intel-local

This will be extracted and installed at "/usr/local" folder. GCC will be used to compile source codes.

Define your environment variable as similar this (This is for bourne shell. If you use another shell refer its man pages and look for 'changing environment variables' )

#LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/lib:/lib
#PATH=$PATH:/usr/bin:/usr/local/bin:/usr/ccs/bin:/usr/ucb
#export LD_LIBRARY_PATH PATH

INSTALLING APACHE AND PHP

Change your directory to "apache-1.3.27" and configure Apache.

ATTENTION : --server-uid parameter value corresponds to the name of Oracle User ('oracle' in this tutorial) and --server-gid parameter corresponds to the primary group of Oracle User ('dba' in this turorial). You have change these values according to your  Oracle username and group.

(Change this configuration parameters according to your needs. Refer INSTALL file in APACHE Source folder or type configure --help)

# cd apache-1.3.27
# LIBS=-lpthread CC=gcc \
	configure --prefix=/usr/local/apache \
	--server-uid=oracle \
	--server-gid=dba \
	--enable-shared=max \
	--enable-module=most

#make

#make install

If you have an error at this stage you have to solve the problem and try again.

Then change your directory to "php-4.3.2" and configure PHP.

(Change this configuration parameters according to your needs. Refer INSTALL file in PHP Source folder or type configure --help)

# cd php-4.3.2
# CC=gcc CPPFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib \
    configure --with-apxs=/usr/local/apache/bin/apxs \
        --with-config-file-path=/usr/local/apache/conf \
        --with-oci8=/opt/oracle/product/8.1.7 \
        --with-oracle=/opt/oracle/product/8.1.7 \
        --disable-libgcc

#make

#make install

If you have an error at this stage you have to solve the problem and try again.

APACHE AND PHP CONFIGURATIONS

If you have make all right, you copy "php.ini-dist" to "/usr/local/apache/conf/" directory named as "php.ini".

#cp php.ini-dist /usr/local/apache/conf/php.ini

Edit this file according to your needs by using any text editor. Especially look for

output_buffering=Off

line and change it as follows:

output_buffering=4096

You also have to edit "httpd.conf" file of Apache Web Server. Open "httpd.conf" file by using any text editor and add the following lines to the very end of the file:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Additionaly change

#ServerName xxxxxx

line. xxxxxx refers to your computer name. Remove '#' sign and add your hostname with xxxxxx. As an example, this line will look like similarly:

ServerName gokmen.selcuk.edu.tr

Create a file named "info.php" in your "DocumentRoot" directory and add this line in it:

#touch /usr/local/apache/htdocs/info.php
#echo "<? echo phpinfo(); ?>" >> /usr/local/apache/htdocs/info.php 

STARTING APACHE AND TESTING

If you made all right, you are ready to start apache by typing:

#/usr/local/apache/bin/apachectl start

Now, open your web browser and type following in address bar:

http://<your_hostname>/info.php

and look for "oci8" and "oracle" section in this php information page loaded. If you see these section you completed everything succssfully.

ESCAPING FROM OCI() ERRORS

I assume that you have already a PHP file for use to connect Oracle. Create or Copy any one of them into "DocumentRoot" ("/usr/local/apache/htdocs" in here) and try to run it in your browser.

I hope you did not get an error like

Warning: ocilogon(): _oci_open_server: Error while trying to retrieve text for error ORA-12545 in ....

This error is telling you did not specify "ORACLE_SID" in your script or

Warning: ocilogon(): _oci_open_server: Error while trying to retrieve text for error ORA-12154 in ...

This error could not find any match "ORACLE_SID" you specified in "TNSNAMES.ORA" file.

If you got one of these error, your environment variables must introduce to your apache web server.

Remember that you compiled apache with Oracle user and group. So, do the following:

create a file named "oraenv" in "/usr/local/apache/conf/" directory and put the following lines in it:

(IMPORTANT : Please change <ORAHOME> with $ORACLE_HOME and <ORASID> with $ORACLE_SID environment variables of your Oracle user..)

Here is an example for this "oraenv" file:

LD_LIBRARY_PATH=/opt/oracle/product/8.1.7/lib:/usr/local/apache/lib:/usr/local/lib
ORACLE_HOME=/opt/oracle/product/8.1.7
ORACLE_SID=/opt/oracle/product/8.1.7
TNS_ADMIN=/opt/oracle/product/8.1.7/network/admin
NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1 		#for English
#NLS_LANG=TURKISH_TURKEY.WE8ISO8859P9 		#for Turkish
ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data

If you want You may insert these lines into "/usr/local/apache/bin/apachectl" file directly.

Now You need to edit "/usr/local/apache/bin/apachectl" file. Open it by a text editor and add the following lines to anywhere in the "CONFIGURATION SECTION"

if [ -f /usr/local/apache/conf/oraenv ] ;
then
    . /usr/local/apache/conf/oraenv
fi

IMPORTANT NOTES :

  • Make sure Oracle Listener and Oracle Database have already started while Apache Server starting/restarting.
  • "oraenv" file will be included in all attempting to start or restart apache web server. Don't delete this file anyway.)
  • In all of your php scripts, you need to add "ORACLE_SID" at connection line like (Replace "orcl" with your ORACLE_SID.) I recommend you to use a connection function as follows and require it in your scripts:
  • <?
    
    class oraClass
    {
        var $sql, $stmt, $conn, $nrows, $ncols, $errmsg, $user, $pass;
    
    
        function db_connect()
        {
            if($this->conn = OCILogon($this->user, $this->pass, $_ENV['ORACLE_SID']))
                $this->db_parse();
            else
                $this->get_error($this->conn);
        }
        .
        .
        .
    ?>
    

    Now you can use your great database with your great web development language.

    If you have any question or suggestions drop an email to my mailbox.

    I hope this tutorial will help you too...

    Contact Information :

    Mustafa GOKMEN
    gokmen@elektronet.net
    http://gokmen.elektronet.net
    Elektronet Ltd. Konya / TURKIYE