Monday, June 10, 2013

Connect PHP with Oracle database 11g xe

Connect PHP with Oracle database 11g xe




Refrence Link :

http://st-curriculum.oracle.com/obe/db/11g/r2/prod/appdev/opensrclang/phphol2010_db/php_db.htm


Purpose

This tutorial shows you how to use PHP with Oracle Database 11g.

Overview

PHP is a popular web scripting language, and is often used to create database-driven web sites. This tutorial helps you get started with PHP and Oracle Database by showing how to build a web application and by giving techniques for using PHP with Oracle.

Prerequisites
  • Make sure your oracle database 11g XE is installed and running.
  • Wamp sever should be installed and working properly to execute PHP codes.
  • Download appropriate oracle instant client ( OS dependent )

First of all lets assume that i am using windows 7 64bit along with oracle database 11G XE 11.2.0.2 and wamp server 2.1. so i have already downloaded Oracle instant client x64. if you are using 32bit windows then download 32bit client otherwise it wont work.

How to configure oracle database to connect with PHP.


Step 1:
Extract all the content of Oracle instant client and copy all the .DLL files and paste into the directory like this 
D:\wamp\bin\apache\Apache2.2.17\bin 

Step 2:

Turn on the Wamp server and left click on the icon > php > PHP extension > check the php_oci8_11g only.



if you are using oracle database older then 11g like 10g then you must check on php_oci8 only.



Step 3:

Turn up the oracle database 11g XE services.



Step 4:
write a PHP code to Connect oracle with database 11g XE , and save the code like testoracle.php extension in www directory of wamp server. Below is the sample
<?php $conn = oci_connect('hr', 'hr', '//localhost/xe'); if (!$conn) {
   $m = oci_error();
   echo $m['message'], "\n";
   exit;
} else {
   print "Connected to Oracle!";
} oci_close($conn); ?>

 
Step 5:
Execute the code the simple typing the localhost/testoracle.php

6 comments:

  1. Thanks, very interesting. But can anyone be more specific, in the matter of where are these peaces of software placed. As PHP do not function like java on the local machine, but on the server, this page describe an apache server (wamp) on a windows server connecting to an oracle remote server (nevermind the OS). That is why it needs an Oracle Instant client, placed on windows machine. But If the Apache server is on the same machine as Oracle is, there is no need of Instant client. How do you connect via a simple browser through PHP to oracle, when both Oracle and Apache PHP are on the same remote server?
    Thank you

    ReplyDelete
    Replies
    1. Have you found an answer for this? I am having same concerns.

      Delete
    2. Dear Mr.Laurentiu Buftea, W Smitty!
      Greeting
      You can use oci_connect
      resource oci_connect ( string $username , string $password [, string $connection_string [, string $character_set [, int $session_mode ]]] )
      username
      The Oracle user name.
      password
      The password for username.
      connection_string
      The tnsnames.ora file can be in the Oracle Net search path, which includes $ORACLE_HOME/network/admin
      The oci_connect() function contains the username, the password and the connection string. In this case, Oracle's Easy Connect connection string syntax is used. It consists of the hostname and the DB service name.
      The oci_close() function closes the connection. Any standard connections not explicitly closed will be automatically released when the script ends.



      Open a Web browser and enter the following URL to display the output:
      http://localhost/~phphol/connect.php
      "Connected to Oracle!" is displayed if the connection succeeds.

      Delete
  2. Dear Mr.Laurentiu Buftea, W Smitty!
    Greeting
    You can use oci_connect
    resource oci_connect ( string $username , string $password [, string $connection_string [, string $character_set [, int $session_mode ]]] )
    username
    The Oracle user name.
    password
    The password for username.
    connection_string
    The tnsnames.ora file can be in the Oracle Net search path, which includes $ORACLE_HOME/network/admin
    The oci_connect() function contains the username, the password and the connection string. In this case, Oracle's Easy Connect connection string syntax is used. It consists of the hostname and the DB service name.
    The oci_close() function closes the connection. Any standard connections not explicitly closed will be automatically released when the script ends.



    Open a Web browser and enter the following URL to display the output:
    http://localhost/~phphol/connect.php
    "Connected to Oracle!" is displayed if the connection succeeds.

    ReplyDelete
  3. How to Connect Oracle Database in PHP? Contact to Remote DBA Service
    For a new user surely it becomes impossible to connect Oracle Database in PHP because it requires lots of skills. However, being a best support provider Cognegic offer Database Administration for Oracle and Online Oracle DB Support for those users who want to connect their Oracle Database in PHP. Here our experts consistent tracking the entire database operations and prevent the problems before they happen. With Oracle Database Solution we enhance the productivity and allow you to dedicate your resource to different areas of business.
    For More Info: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete