skip to Main Content

I am struggling to connect my PHP application to an Oracle database. I have tried multiple options over the last week with no success.

I have managed to install oci8 extension but the oci_connect function is not recognised.

Please assist.

RedHat Enterprise Linux 7.6
PHP: 7.3
Oracle: 12c

2

Answers


  1. Chosen as BEST ANSWER

    Having struggled for over a week to figure this out, here is a summary of what ultimately worked. I hope this helps somebody else.

    These instructions apply to RedHat / CentOS Linux installations. My experience was that the instructions for RedHat / CentOS were slightly different to other Linux installations. AVOID the Oracle guidance... They did not help at all!

    Step1: I followed the instructions on this excellent website to install PHP 7.3 and additional Remi rpm packages:https://tecadmin.net/install-php7-on-centos7/

    Step 2: I then used the Remi repository (see step 1) to install the oci8 extension:

    $ sudo yum --enablerepo=remi-php73 install php-oci8
    

    Step 3: We now need to install the oracle instant client 18.3 packages. This is very nicely explained on this website: https://qiita.com/tkprof/items/2a4eb868f45fb5759110

    $ cd /etc/yum.repos.d
    $ sudo wget http://yum.oracle.com/public-yum-ol7.repo
    $ sudo wget http://yum.oracle.com/RPM-GPG-KEY-oracle-ol7
    $ sudo rpm --import RPM-GPG-KEY-oracle-ol7
    $ sudo yum-config-manager --enable ol7_oracle_instantclient
    $ sudo yum install oracle-instantclient18.3-basic
    $ sudo yum install oracle-instantclient18.3-devel
    $ sudo yum install oracle-instantclient18.3-jdbc
    $ sudo yum install oracle-instantclient18.3-sqlplus
    $ sudo yum list oracle-instantclient*
    

    Step 4: The oracle files have been created in /usr/lib/oracle/18.3 We now need to create a symlink so that the oracle files are included when the server is running:

    $ sudo sh -c "echo /usr/lib/oracle/18.3/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf"
    $ sudo ldconfig
    

    Step 5: Restart the server

     $ sudo systemctl stop httpd
     $ sudo systemctl start httpd
    

    Step 6: We can now create a simple php file that tests the connection to Oracle:

    <?php     
    $conn = oci_connect("username", "password", "//url/SID");
    
    if (!$conn) {
        echo "oci8 working! However the following errors occurred: <br>";
        $m = oci_error();
        echo $m['message'], "n";
        exit;
    } else {
        print "Connected to Oracle!";
    }
    // Close the Oracle connection
    oci_close($conn);
    

    If you get a system error then the installation has not worked.


  2. Update for latest Oracle Instant Client, version 19.3 for PHP-OCI8:

    >> wget http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/getPackage/oracle-instantclient19.3-basic-19.3.0.0.0-1.x86_64.rpm
    >> wget http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/getPackage/oracle-instantclient19.3-devel-19.3.0.0.0-1.x86_64.rpm
    >> wget http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/getPackage/oracle-instantclient19.3-jdbc-19.3.0.0.0-1.x86_64.rpm
    >> wget http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/getPackage/oracle-instantclient19.3-sqlplus-19.3.0.0.0-1.x86_64.rpm
    
    >> sudo yum localinstall oracle-instantclient19.3-basic-19.3.0.0.0-1.x86_64.rpm oracle-instantclient19.3-devel-19.3.0.0.0-1.x86_64.rpm oracle-instantclient19.3-jdbc-19.3.0.0.0-1.x86_64.rpm oracle-instantclient19.3-sqlplus-19.3.0.0.0-1.x86_64.rpm
    
    // create symbolic link
    eu-onesead-d001 ~]# sudo sh -c "echo /usr/lib/oracle/19.3/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf"
    eu-onesead-d001 ~]# sudo ldconfig
    

    Restart the server

    To test the connection, I have created a php file which I upload into the server and test via the browser. NOTE: you should obviously use appropriate caution and delete the file after testing:

    <?php
    echo "Ensure you have restarted the server if you have installed any new extensions!";
    echo "<br>Attempting Oracle db connection...";
    
    // this will open the oracle extension and execute a connection
    try {
    $conn = oci_connect(
        "username_goes_here",
        "password_goes_here",
        "//oracle_server_address_goes_here/sid_goes_here");
    
    echo "<br>OCI Connect extension found.. opening connnection...";
    
    if (!$conn) {
        echo "<br>Function 'oci_connect' found and working but cannot connect to Oracle: <br>";
        $m = oci_error();
        echo $m['message'], "n";
        exit;
    } else {
        echo "<br>SUCCESS: Connected to Oracle!";
    }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    
    // Close the Oracle connection
    oci_close($conn);
    echo "<br>Connection closed";
    echo "<br>For security, please delete this file off the server";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search