Can't get ODBC to work with PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • webhead74@gmail.com

    Can't get ODBC to work with PHP

    Hi, I've worked on this for a couple of days & can't figure out what
    I'm doing wrong.

    I'm trying to connect to a remote AS400 database using odbc & php.
    Here's my environment:

    Server: RedHat ES 2.1

    IBM HTTP Server (was pre-built by IBM):
    Server version: IBM_HTTP_Server/2.0.47.1 Apache/2.0.47
    Server built: May 20 2004 16:46:47
    Build level: WWWIHS2047/web_IHS0420.04
    Server's Module Magic Number: 20020903:4
    Architecture: 32-bit
    Server compiled with....
    -D APACHE_MPM_DIR= "server/mpm/worker"
    -D APR_HAS_SENDFIL E
    -D APR_HAS_MMAP
    -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
    -D APR_USE_SYSVSEM _SERIALIZE
    -D APR_USE_PTHREAD _SERIALIZE
    -D SINGLE_LISTEN_U NSERIALIZED_ACC EPT
    -D APR_HAS_OTHER_C HILD
    -D AP_HAVE_RELIABL E_PIPED_LOGS
    -D HTTPD_ROOT="/opt/IBMIHS"
    -D SUEXEC_BIN="/opt/IBMIHS/bin/suexec"
    -D DEFAULT_SCOREBO ARD="logs/apache_runtime_ status"
    -D DEFAULT_ERRORLO G="logs/error_log"
    -D AP_TYPES_CONFIG _FILE="conf/mime.types"
    -D SERVER_CONFIG_F ILE="conf/httpd.conf"

    PHP 4.3.10, configured with:
    ../configure --prefix=/usr/local/php --with-apxs2=/opt/IBMIHS/bin/apxs
    --with-mysql=/usr/local/mysql
    --with-mysqli=/usr/local/mysql/bin/mysql_config
    --with-libxml-dir=/usr/lib --with-ibm-db2=/opt/IBM/db2/V8.1
    --with-unixODBC=/usr/local

    unixODBC v2.2.11 compiled w/ defaults per the INSTALL file

    IBM iSeriesODBC v5.1.0-0.16 installed by RPM


    My odbc.ini & odbcinst.ini are both in /usr/local/etc
    [color=blue]
    >From the command line on the server, I can query the AS400 via unixODBC[/color]
    (where foo = dsn):

    #isql -v foo
    +---------------------------------------+
    | Connected! |
    | |
    | sql-statement |
    | help [tablename] |
    | quit |
    | |
    +---------------------------------------+
    SQL>
    [color=blue]
    >From my php script, I get:[/color]

    Warning: odbc_connect(): SQL error: [IBM][CLI Driver] SQL1013N The
    database alias name or database name "foo" could not be found.
    SQLSTATE=42705 , SQL state 08001 in SQLConnect in
    /opt/IBMIHS/htdocs/en_US/test.php on line 7
    Cannot connect to the database.

    So obviously PHP isn't finding the datasource. Why? What do I need to
    do to configure PHP to use the DSNs in odbc.ini?

    Just for giggles, I've added

    ODBCINI=/usr/local/etc/odbc.ini
    export ODBCINI

    to /opt/IBMIHS/bin/envvars thinking that would let apache pick up the
    odbc.ini... doesn't make a difference.

    For what it's worth, here's my PHP code:

    <?

    $username = "user";
    $password = "pass";
    $host = "foo";

    $connect = odbc_connect("$ host", "$username" , "$password" ) or
    die("Cannot connect to
    the database.<br>" . mysql_error()); ;

    $query = "SELECT * FROM QLIB.FOO";

    $result = odbc_exec($conn ect, $query);

    while(odbc_fetc h_row($result)) {
    $LEMP = odbc_result($re sult, 2);
    print("<b>NAME: </b>$LEMP<br><b>A DDRESS:</b>foo<br><br>\n ");
    }

    odbc_close($con nect);
    ?>

    Same code works fine on a Windows box w/ apache & php with a windows
    DNS...

    I've read the unixODBC docs, the IBM docs, the php docs... can't figure
    out what I'm missing..

    Help?

    Thanks,
    Kelly

  • Steve

    #2
    Re: Can't get ODBC to work with PHP

    webhead74@gmail .com wrote:[color=blue]
    > Hi, I've worked on this for a couple of days & can't figure out what
    > I'm doing wrong.
    >
    > I'm trying to connect to a remote AS400 database using odbc & php.
    > Here's my environment:
    >
    > Server: RedHat ES 2.1
    >
    > IBM HTTP Server (was pre-built by IBM):
    > Server version: IBM_HTTP_Server/2.0.47.1 Apache/2.0.47
    > Server built: May 20 2004 16:46:47
    > Build level: WWWIHS2047/web_IHS0420.04
    > Server's Module Magic Number: 20020903:4
    > Architecture: 32-bit
    > Server compiled with....
    > -D APACHE_MPM_DIR= "server/mpm/worker"
    > -D APR_HAS_SENDFIL E
    > -D APR_HAS_MMAP
    > -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
    > -D APR_USE_SYSVSEM _SERIALIZE
    > -D APR_USE_PTHREAD _SERIALIZE
    > -D SINGLE_LISTEN_U NSERIALIZED_ACC EPT
    > -D APR_HAS_OTHER_C HILD
    > -D AP_HAVE_RELIABL E_PIPED_LOGS
    > -D HTTPD_ROOT="/opt/IBMIHS"
    > -D SUEXEC_BIN="/opt/IBMIHS/bin/suexec"
    > -D DEFAULT_SCOREBO ARD="logs/apache_runtime_ status"
    > -D DEFAULT_ERRORLO G="logs/error_log"
    > -D AP_TYPES_CONFIG _FILE="conf/mime.types"
    > -D SERVER_CONFIG_F ILE="conf/httpd.conf"
    >
    > PHP 4.3.10, configured with:
    > ./configure --prefix=/usr/local/php --with-apxs2=/opt/IBMIHS/bin/apxs
    > --with-mysql=/usr/local/mysql
    > --with-mysqli=/usr/local/mysql/bin/mysql_config
    > --with-libxml-dir=/usr/lib --with-ibm-db2=/opt/IBM/db2/V8.1
    > --with-unixODBC=/usr/local
    >
    > unixODBC v2.2.11 compiled w/ defaults per the INSTALL file
    >
    > IBM iSeriesODBC v5.1.0-0.16 installed by RPM
    >
    >
    > My odbc.ini & odbcinst.ini are both in /usr/local/etc
    >[color=green]
    >>From the command line on the server, I can query the AS400 via unixODBC[/color]
    > (where foo = dsn):
    >
    > #isql -v foo
    > +---------------------------------------+
    > | Connected! |
    > | |
    > | sql-statement |
    > | help [tablename] |
    > | quit |
    > | |
    > +---------------------------------------+
    > SQL>
    >[color=green]
    >>From my php script, I get:[/color]
    >
    > Warning: odbc_connect(): SQL error: [IBM][CLI Driver] SQL1013N The
    > database alias name or database name "foo" could not be found.
    > SQLSTATE=42705 , SQL state 08001 in SQLConnect in
    > /opt/IBMIHS/htdocs/en_US/test.php on line 7
    > Cannot connect to the database.
    >
    > So obviously PHP isn't finding the datasource. Why? What do I need to
    > do to configure PHP to use the DSNs in odbc.ini?
    >
    > Just for giggles, I've added
    >
    > ODBCINI=/usr/local/etc/odbc.ini
    > export ODBCINI
    >
    > to /opt/IBMIHS/bin/envvars thinking that would let apache pick up the
    > odbc.ini... doesn't make a difference.
    >
    > For what it's worth, here's my PHP code:
    >
    > <?
    >
    > $username = "user";
    > $password = "pass";
    > $host = "foo";
    >
    > $connect = odbc_connect("$ host", "$username" , "$password" ) or
    > die("Cannot connect to
    > the database.<br>" . mysql_error()); ;
    >
    > $query = "SELECT * FROM QLIB.FOO";
    >
    > $result = odbc_exec($conn ect, $query);
    >
    > while(odbc_fetc h_row($result)) {
    > $LEMP = odbc_result($re sult, 2);
    > print("<b>NAME: </b>$LEMP<br><b>A DDRESS:</b>foo<br><br>\n ");
    > }
    >
    > odbc_close($con nect);
    > ?>
    >
    > Same code works fine on a Windows box w/ apache & php with a windows
    > DNS...
    >
    > I've read the unixODBC docs, the IBM docs, the php docs... can't figure
    > out what I'm missing..
    >
    > Help?
    >
    > Thanks,
    > Kelly
    >[/color]
    I think the quotes around the user/pass/db fields are a bit of overkill,
    and using odbc_error instead of mysql_error may shed a bit more light on
    the problem???

    Steve

    Comment

    Working...