Sunday, August 1, 2010

Connecting to oracle 10g from a remote pc using php

( all the work may be same if connecting to a database at localhost, i didn't try it )

You have to do the following-----

assuming that no php,mysql,apache server is installed in your machine . then just  download wampserver and install it .
it has all the three items I mentioned earlier .

download Instant Client Package - Basic file from here ( i used version 10.2.0.4 , windows 32-bit ) and extract it under some folder say

C:\Program Files\OracleClient ( no folder under this directory only the dll and jar files )

then add the directory mentioned ( that means the directory where you extracted the files   ) above to your PATH environment variable . you can do it here

start >> My Computer >> Properties ( clicking  right button on My Computer ) >> Advanced >> Environment Variable >> System variables 

PATH variable may already defined in your pc then just Edit it by adding a semicolon and the directory where   Instant Client Package - Basic is extracted  otherwise add it .

( while comparing ignore case , all "PATH" , "Path" , "path" means the same because environment variable is case insensitive  )

open php.ini from here

                   ..................\wamp\bin\apache\Apache2.2.11\bin\ and remove the starting semicolon ( uncomment the following line )

                   extension=php_oci8.dll ( if using oracle 10g )
                   extension=php_oci8_11g.dll ( if using oracle 11g )
but don't do the both.

( you can also edit the file by clicking wamp's task-bar icon then focusing on php  you will get a pop-up menu there you will have a  php.ini file   )



then save the file  and restart your pc .

then start wampserver If you are lucky enough then you will not have any error . That  means inclusion of oci8 module was successful.

you can verify by this..........

click here http://localhost      and you will go to wampserver home page then you will get 
phpinfo() ( you can also try to find oci8 in Loaded Extensions at wamp's homepage )

click it and try to find something like the following .







                                                                                               oci8

OCI8 Supportenabled
Version1.3.5
Revision$Revision: 1.269.2.16.2.38.2.32 $
Active Persistent Connections0
Active Connections0
Oracle Instant Client Version10.2
Temporary Lob supportenabled
Collections supportenabled







DirectiveLocal ValueMaster Value
oci8.connection_classno valueno value
oci8.default_prefetch100100
oci8.eventsOffOff
oci8.max_persistent-1-1
oci8.old_oci_close_semanticsOffOff
oci8.persistent_timeout-1-1
oci8.ping_interval6060
oci8.privileged_connectOffOff
oci8.statement_cache_size2020
                   
to check your database  connection use the following code  inside the php tag










$conn = oci_connect('username', 'password', 'ip address/service_name');

if (!$conn) 
{
   trigger_error("Could not connect to database", E_USER_ERROR);
}

else
{
   echo "Connected" ;
}










  • copy it to .... \wamp\www\yourfilename.php file 
  • change the username , password , according to yours .
  • service name - according to your Service name  
  • check the link 
                        http://localhost/yourfilename.php

if the page shows connected that means you are connected to the database.

you may need to download MSVCR71.dll and copy file to the directory where all the Instant Client Package - Basic files are situated.