This discussion is archived
4 Replies Latest reply: Nov 21, 2008 9:33 AM by 244142 RSS

PHP - Oracle Connection Problem

669955 Newbie
Currently Being Moderated
Hi Everyone,
I am trying to connect Oracle database with PHP in Windows enviornment. For that I did the following works.

1) Installed Oracle10g R2 client.

2) Create an SystemDSN in ODBC for the Oracle database and test the connection - working fine.

(ODBC connection is working fine with the Oracle Client installed)

3) Download the latest PHP, PHP 5.2.6 zip package from http://sg2.php.net/downloads.php

4) Copy the files into d:\php\ folder.

5) Create a simple php file to make an odbc connection, test.php in the same php directory, and this is the file content:
<?php
odbc_connect("ebook","ebook123","ebookdev");
?>

6) When I tried to ececute woth php.exe file this is the error message I'm getting....
D:\php>php.exe test.php
Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager] Data source
name not found and no default driver specified, SQL state IM002 in SQLConnect i
n D:\php-5.2.6-Win32\test.php on line 3
D:\php-5.2.6-Win32>

7) When I uncomment *;extension=php_pdo_odbc.dll* this entry in php.ini and just put the command php on the command line, then it pop up wondows error message box, Same erroe comimg..

8) When I uncmoomnt *;extension=php_oci8.dll and ;extension=php_pdo_oci.dll* then following erroe message appearing on command line.
D:\php>php
PHP Warning: PHP Startup: Unable to load dynamic library './ext/php_oci8.dll' -
%1 is not a valid Win32 application.
in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library './ext/php_pdo_oci.dll
' - %1 is not a valid Win32 application.
in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library './ext/php_pdo_oci8.dl
l' - %1 is not a valid Win32 application.
in Unknown on line 0
Also it pops up wondows error message of, "This application failed to start because OCIW32.dll was not found. Re-installing the application may fix this problem" and "This application failed to start because OCI.dll was not found. Re-installing the application may fix this problem"

Please adwise me to sort out this. How ever I want to get connect the Oracle database (either through ODBC or Direct) by PHP calls.

Thanks..

SURESH

Edited by: user10545483 on Nov 11, 2008 9:30 PM
  • 1. Re: PHP - Oracle Connection Problem
    164043 Explorer
    Currently Being Moderated
    Hi Suresh!
    The error message you're getting ("%1 is not a valid Win32 application") probably means that there is something
    wrong with the DLL you downloaded. You should download it again, from http://pecl4win.php.net/ext.php/php_oci8.dll

    That is the OCI8. I advise against PDO as it is immature and inferior in capabilities to the native OCI8 version. Not so
    long ago, it wasn't possible to describe cursor, which would severely limit the usefulness of it.
  • 2. Re: PHP - Oracle Connection Problem
    669955 Newbie
    Currently Being Moderated
    Thanks for the reply.

    I dowloaded a fresh copy of php_oci8.dll from http://pecl4win.php.net/ext.php/php_oci8.dll
    Still the same problem.

    I I type the folloing command in the command line,
    D:\php>php

    First it pop up a windows error message box, +"This application failed to start because OCI.dll was not found. Re-installing the application may fix this problem"+

    Also the same error message on the comand window:
    PHP Warning: PHP Startup: Unable to load dynamic library './ext/php_oci8.dll' -
    +%1 is not a valid Win32 application.+
    in Unknown on line 0
  • 3. Re: PHP - Oracle Connection Problem
    164043 Explorer
    Currently Being Moderated
    Did you set extension_dir in your php.ini file?
  • 4. Re: PHP - Oracle Connection Problem
    244142 Newbie
    Currently Being Moderated
    I would have had no problem if I had:

    (1) used ZEND
    (2) installed php, apache, oracle XE, instant client

    I installed 11g1 Enterprise and that already had a client.

    Fedora already had php 5 and apache 2

    FINALLY: The following works in the browser.

    hr/hr is automatically built in the 11g1 database

    <?php // File: anyco.php

    require('anyco_ui.inc');

    $db = "(DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
    (CONNECT_DATA =
    (SERVER = DEDICATED)
    (SID = LMKIIIGDNSID)
    )
    )";
    // oci_internal_debug(1); // turn on tracing
    // $conn = oci_connect("/", "", null, null, OCI_SYSDBA);
    // if ($conn=oci_connect('hr', 'hr','LMKIIIGDNSID'))
    if ($conn=oci_connect('hr', 'hr',$db))
    {
    echo "Successfully connected to Oracle.\n";
    // OCILogoff($conn);
    }
    else
    {
    $err = OCIError();
    echo "Oracle Connect Error " . $err['message'];
    }

    ui_print_header('Departments');
    do_query($conn, 'SELECT * FROM DEPARTMENTS');
    ui_print_footer(date('Y-m-d H:i:s'));

    // Execute query and display results
    function do_query($conn, $query)
    {
    $stid = oci_parse($conn, $query);
    $r = oci_execute($stid, OCI_DEFAULT);

    print '<table border="1">';
    while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    print '<tr>';
    foreach ($row as $item) {
    print '<td>'.
    ($item ? htmlentities($item) : ' ').'</td>';
    }
    print '</tr>';
    }
    print '</table>';
    }

    ?>


    What did I do:

    in the file /etc/rc.d/init.d/httpd

    I inserted statements starting with export position indicated by the surrounding code.

    # Path to the apachectl script, server binary, and short-form for messages.
    apachectl=/usr/sbin/apachectl
    httpd=${HTTPD-/usr/sbin/httpd}
    prog=httpd
    pidfile=${PIDFILE-/var/run/httpd.pid}
    lockfile=${LOCKFILE-/var/lock/subsys/httpd}
    RETVAL=0
    # new statements here
    export ORACLE_HOSTNAME=localhost.localdomain
    export TNS_ADMIN=/u01/app/oracle/product/11.1.0/db_1/network/admin
    export ORACLE_BASE=/u01/app/oracle

    export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
    export ORACLE_SID=LMKIIIGDNSID
    export LD_LIBRARY_PATH=/u01/app/oracle/product/11.1.0/db_1/lib

    # new statements above

    # The semantics of these two functions differ from the way apachectl does
    # things -- attempting to start while running is a failure, and shutdown
    # when not running is also a failure. So we just do it the way init scripts
    # are expected to behave here.
    start() {
    echo -n $"Starting $prog: "
    LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
    return $RETVAL
    }

    I had put similar statements in /etc/httpd/conf/httpd.conf (setenv). I will remove these statements (and see if it still works!)

    It stopped working...so:

    into /etc/httpd/conf/httpd.conf I had to add:
    ### Section 1: Global Environment
    #
    # The directives in this section affect the overall operation of Apache,
    # such as the number of concurrent requests it can handle or where it
    # can find its configuration files.
    #

    SetEnv ORACLE_HOSTNAME localhost.localdomain
    SetEnv TNS_ADMIN /u01/app/oracle/product/11.1.0/db_1/network/admin
    SetEnv ORACLE_BASE /u01/app/oracle
    SetEnv ORACLE_HOME /u01/app/oracle/product/11.1.0/db_1
    SetEnv ORACLE_SID lmkiiiGDNSID
    SetEnv ORACLE_TERM xterm
    SetEnv LD_LIBRARY_PATH /u01/app/oracle/product/11.1.0/db_1/lib

    #
    # Don't give away too much information about all the subcomponents
    # we are running. Comment out this line if you don't mind remote sites
    # finding out what major optional modules you are running
    ServerTokens OS

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points