[unixODBC-support] Doing a SQLDriverConnect fails in Apache with Error 500: "Internal Server Error"

Miguel Rentes miguel.rentes at efacec.pt
Tue Jun 3 14:54:19 BST 2008


Hi Stefan,

I already turned on ODBC tracing and when I try to run the .cgi I have 
an IM004 error:

[ODBC][21523][__handles.c][447]
        Exit:[SQL_SUCCESS]
            Environment = 0x83f6090
[ODBC][21523][SQLSetEnvAttr.c][182]
        Entry:           
            Environment = 0x83f6090           
            Attribute = SQL_ATTR_ODBC_VERSION           
            Value = 0x3           
            StrLen = 0
[ODBC][21523][SQLSetEnvAttr.c][349]
        Exit:[SQL_SUCCESS]
[ODBC][21523][SQLAllocHandle.c][358]
        Entry:
            Handle Type = 2
            Input Handle = 0x83f6090
[ODBC][21523][SQLAllocHandle.c][476]
        Exit:[SQL_SUCCESS]
            Output Handle = 0x83f6810
[ODBC][21523][SQLDriverConnect.c][678]
        Entry:           
            Connection = 0x83f6810           
            Window Hdl = (nil)           
            Str In = [DSN=SXDB;PWD=******;][length = 20 
(SQL_NTS)]           
            Str Out = 0xbfeb1af0           
            Str Out Max = 2048           
            Str Out Ptr = (nil)           
            Completion = 0
        UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'

[ODBC][21523][SQLConnect.c][1315]Error: IM004
[ODBC][21523][SQLAllocHandle.c][523]
        Entry:
            Handle Type = 3
            Input Handle = 0x83f6810
[ODBC][21523][SQLAllocHandle.c][549]Error: 08003
[ODBC][21523][SQLTables.c][175]Error: SQL_INVALID_HANDLE
[ODBC][21523][SQLNumResultCols.c][130]Error: SQL_INVALID_HANDLE
[ODBC][21523][SQLFetch.c][141]Error: SQL_INVALID_HANDLE
[ODBC][21523][SQLFreeHandle.c][352]Error: SQL_INVALID_HANDLE
[ODBC][21523][SQLFreeHandle.c][273]
        Entry:
            Handle Type = 2
            Input Handle = 0x83f6810
[ODBC][21523][SQLFreeHandle.c][324]
        Exit:[SQL_SUCCESS]
[ODBC][21523][SQLFreeHandle.c][206]
        Entry:
            Handle Type = 1
            Input Handle = 0x83f6090

I don't know much about ODBC but this error is telling me that ODBC 
can't allocate a SQL_HANDLE_HENV handle? If so, what do I have t do to 
eliminate this error?

This is my simple .cgi code:

#include <stdio.h>
#include <sql.h>
#include <sqlext.h>


void extract_error(
    char *fn,
    SQLHANDLE handle,
    SQLSMALLINT type);

void extract_error(
    char *fn,
    SQLHANDLE handle,
    SQLSMALLINT type)
{
    SQLINTEGER     i = 0;
    SQLINTEGER     native;
    SQLCHAR     state[ 7 ];
    SQLCHAR     text[256];
    SQLSMALLINT     len;
    SQLRETURN     ret;

    memset(text,0,256);

    fprintf(stderr,
            "\n"
            "The driver reported the following diagnostics whilst running "
            "%s\n\n",
            fn);

    do
    {
        ret = SQLGetDiagRec(type, handle, ++i, state, &native, text, 
sizeof(text), &len );
        if (!SQL_SUCCEEDED(ret))
            printf("%s:%ld:%ld:%s\n", state, i, native, text);
    }
    while( ret == SQL_SUCCESS );

}



main() {
    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN ret; /* ODBC API return status */
    SQLSMALLINT columns; /* number of columns in result-set */
    int row = 0;

//MLR - cgi stuff
printf("Content-Type: text/html\n\n");
printf("<html>\n");
printf("<head>");
printf("<link rel=\"stylesheet\" href=\"../styles.css\" 
type=\"text/css\">");

/* Allocate an environment handle */
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
/* We want ODBC 3 support */
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
/* Allocate a connection handle */
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
/* Connect to the DSN mydsn */
/* You will need to change mydsn to one you have created and tested */
SQLDriverConnect(dbc, NULL, "DSN=SXDB;PWD=Scatex;", SQL_NTS, NULL, 0, 
NULL, SQL_DRIVER_NOPROMPT);
/* Allocate a statement handle */
SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);

/* Retrieve a list of tables */
SQLTables(stmt, NULL, 0, "SCATEX", SQL_NTS, "MEDIDAS", SQL_NTS, NULL, 0);
/* How many columns are there */
SQLNumResultCols(stmt, &columns);
/* Loop through the rows in the result-set */
while (SQL_SUCCEEDED(ret = SQLFetch(stmt))) {
    SQLUSMALLINT i;
    printf("Row %d\n", row++);
    /* Loop through the columns */
    for (i = 1; i <= columns; i++) {
        SQLINTEGER indicator;
        char buf[512];
        /* retrieve column data as a string */
    ret = SQLGetData(stmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator);
        if (SQL_SUCCEEDED(ret)) {
            /* Handle null columns */
            if (indicator == SQL_NULL_DATA) strcpy(buf, "NULL");
        printf("  Column %u : %s\n", i, buf);
        } else {
        extract_error("SQLAllocHandle for dbc", env, SQL_HANDLE_ENV);
                exit(-1);
    }
    }
}

/* Free all the handles */
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
/* disconnect */
SQLFreeHandle(SQL_HANDLE_DBC, dbc);
SQLFreeHandle(SQL_HANDLE_ENV, env);


printf("</head>");
printf("<body bgcolor=\"#CCCCCC\" align=\"center\" class=\"fText\">\n");

printf("</body>\n</html>\n");

}

Thanks in advance!

Miguel Rentes


Stefan Radman escreveu:
> > Any more ideas on what can be the source of the problem?
> sorry no. does not seem to be a trivial problem.
>  
> possibly writing some debug code into your program and turning on ODBC 
> tracing might help determine what exactly is making your program fail.
>  
> Stefan
>  
>
>     ------------------------------------------------------------------------
>     *From:* Miguel Rentes [mailto:miguel.rentes at efacec.pt]
>     *Sent:* Tuesday, 03 June, 2008 15:34
>     *To:* Stefan Radman
>     *Cc:* Support for the unixODBC project
>     *Subject:* Re: [unixODBC-support] Doing a SQLDriverConnect fails
>     in Apache with Error 500: "Internal Server Error"
>
>     Hi Stefan,
>
>     I have a printenv.cgi that I used before to see the environment
>     variables I have when I make a request with the web browser. Here
>     is the output:
>
>     DOCUMENT_ROOT="/home/scatex/dsv/sxbin/weblists/htdocs"
>     GATEWAY_INTERFACE="CGI/1.1"
>     HTTP_ACCEPT="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
>     HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.7"
>     HTTP_ACCEPT_ENCODING="gzip,deflate"
>     HTTP_ACCEPT_LANGUAGE="pt-pt,pt;q=0.8,en;q=0.5,en-us;q=0.3"
>     HTTP_CACHE_CONTROL="no-cache"
>     HTTP_CONNECTION="keep-alive"
>     HTTP_COOKIE="PHPSESSID=ca37dscif7aeffg42pfn9f45o3"
>     HTTP_HOST="172.18.200.153:8080"
>     HTTP_KEEP_ALIVE="300"
>     HTTP_PRAGMA="no-cache"
>     HTTP_USER_AGENT="Mozilla/5.0 (X11; U; Linux i686; pt-PT; rv:1.9b5) Gecko/2008050509 Firefox/3.0b5"
>     LD_LIBRARY_PATH="/home/oracle/apache/lib:/home/oracle/apache/lib:/home/oracle/oraHome1/lib:/usr/local/lib:/home/scatex/dsv/sxbin"
>     ODBCINI="/usr/local/etc/odbc.ini"
>     ODBCINSTINI="/usr/local/etc/odbcinst.ini"
>     PATH=".:/usr/java/j2sdk1.4.2_09/bin:/usr/sbin:/etc:/bin:/usr/bin:/usr/local/bin:/usr/local/lib:/usr/X11R6/bin:/home/scatex/dsv/sxbin:/home/scatex/dsv/sxbin/scripts:/home/scatex/dsv/sxbin/grafs:/home/scatex/dsv/scatex/synoptic:/home/scatex/dsv/scatex/libsyno:/usr/xrt/bin:/home/oracle/oraHome1/bin"
>     QUERY_STRING=""
>     REMOTE_ADDR="172.18.200.62"
>     REMOTE_PORT="43898"
>     REQUEST_METHOD="GET"
>     REQUEST_URI="/cgi-bin/printenv.cgi"
>     SCRIPT_FILENAME="/home/scatex/dsv/sxbin/weblists/cgi-bin/printenv.cgi"
>     SCRIPT_NAME="/cgi-bin/printenv.cgi"
>     SERVER_ADDR="172.18.200.153"
>     SERVER_ADMIN="you at example.com"
>     SERVER_NAME="172.18.200.153"
>     SERVER_PORT="80"
>     SERVER_PROTOCOL="HTTP/1.1"
>     SERVER_SIGNATURE=""
>     SERVER_SOFTWARE="Apache/2.2.8 (Unix) PHP/5.2.6"
>     TZ="PORST-0PORDT-1,M3.5.0/01:00,M10.5.0/03:00"
>     XAPPLRESDIR="/home/scatex/dsv/sxcli/resources_UK"
>         
>
>     As you can see from LD_LIBRARY_PATH I have all the search path I
>     need. I compiled the .cgi with "cc -o testeODBC.cgi testeODBC.c
>     -lodbc" (libodbc.so is in /usr/local/lib) and it works from the
>     command line environment.
>
>     I'm afraid it isn't a search path problem. I've also checked
>     /etc/ld.so.conf and it has /usr/local/lib (and also added
>     /home/oracle/oraHome1/lib and did a ldconfig) but restarting
>     apache didn't do the job.
>
>     Any more ideas on what can be the source of the problem?
>
>     Regards,
>
>     Miguel Rentes
>
>     Stefan Radman wrote:
>>     If it works from the command line and doesnt work from a daemon
>>     it is often due to different environments.
>>     I would guess that you lack a library search path for loading
>>     Oracle libraries required by
>>     /home/oracle/oraHome1/lib/libsqora.so.10.1
>>      
>>     Stefan
>>
>>         ------------------------------------------------------------------------
>>         *From:* unixodbc-support-bounces at easysoft.com
>>         [mailto:unixodbc-support-bounces at easysoft.com] *On Behalf Of
>>         *Miguel Rentes
>>         *Sent:* Tuesday, 03 June, 2008 12:34
>>         *To:* unixodbc-support at easysoft.com
>>         *Subject:* [unixODBC-support] Doing a SQLDriverConnect fails
>>         in Apache with Error 500: "Internal Server Error"
>>
>>         Hi everyone!
>>
>>         I'm trying to create a .cgi that makes a connection to Oracle
>>         Database using unixODBC (in a Red Hat Enterprise Linux 4
>>         machine).
>>
>>         I've successfully compiled Apache (APR 1.2.12, APR-Util
>>         1.2.12, HTTPD 2.2.8), unixODBC (2.2.12) and PHP (5.2.6) but
>>         when I try to run the cgi from the web browser I get
>>         "Internal Server Error". If I run the same cgi from command
>>         line it works wih no errors.
>>
>>         Here is my actual configuration:
>>
>>         /usr/local/etc/odbc.ini:
>>
>>         [SXDB]
>>         Application Attributes        = T
>>         Attributes        = W
>>         BatchAutocommitMode        = IfAllSuccessful
>>         CloseCursor        = T
>>         DisableDPM        = F
>>         DisableMTS        = T
>>         Driver        = ORACLE_ODBC_Driver_Linux_ora10g
>>         DSN        = SXDB
>>         EXECSchemaOpt        =
>>         EXECSyntax        = T
>>         Failover        = T
>>         FailoverDelay        = 10
>>         FailoverRetryCount        = 10
>>         FetchBufferSize        = 64000
>>         ForceWCHAR        = F
>>         Lobs        = T
>>         Longs        = T
>>         MetadataIdDefault        = F
>>         QueryTimeout        = T
>>         ResultSets        = T
>>         ServerName        = SXDB
>>         SQLGetData extensions        = F
>>         Translation DLL        =
>>         Translation Option        = 0
>>         UserID        = scatex
>>
>>         /usr/local/etc/odbcinst.ini:
>>
>>         [ORACLE_ODBC_Driver_Linux_ora10g]
>>         Description        = Oracle 10g R2 ODBC Driver for Linux
>>         Driver        = /home/oracle/oraHome1/lib/libsqora.so.10.1
>>         Driver64        =
>>         Setup        =
>>         Setup64        =
>>         UsageCount        = 1
>>         CPTimeout        =
>>         CPReuse        =
>>
>>         [ODBC]
>>         Trace        = Yes
>>         TraceFile        = /tmp/sql.log
>>         ForceTrace        = Yes
>>         Pooling        = Yes
>>
>>         When I run iusql I get:
>>
>>         [oracle at pjs-linux php-5.2.6]$ iusql SXDB scatex Scatex
>>         +---------------------------------------+
>>         | Connected!                            |
>>         |                                       |
>>         | sql-statement                         |
>>         | help [tablename]                      |
>>         | quit                                  |
>>         |                                       |
>>         +---------------------------------------+
>>         SQL> select sysdate from dual;
>>         +--------------------+
>>         | SYSDATE            |
>>         +--------------------+
>>         | 2008-06-03 11:26:26|
>>         +--------------------+
>>         SQLRowCount returns -1
>>         1 rows fetched
>>         SQL> quit
>>
>>         I don't know why Apache is giving me Error 500 but if I run
>>         the .cgi from command line everything works fine.
>>
>>         My .cgi is just doing a simple select (using
>>         SQLDriverConnect) and then it disconnects.
>>
>>         >From apache logs I can see:
>>
>>         [Tue Jun 03 11:21:20 2008] [error] [client 172.18.200.62]
>>         Debug: connect_odbc.c [connect_odbc] - ODBC string
>>         'DSN=SXDB;UID=scatex;PWD=Scatex;', referer:
>>         http://172.18.200.153/cgi-bin/get_ret.cgi
>>         [Tue Jun 03 11:21:20 2008] [error] [client 172.18.200.62]
>>         connection string is: DSN=SXDB;UID=scatex;PWD=Scatex;,
>>         referer: http://172.18.200.153/cgi-bin/get_ret.cgi
>>         [Tue Jun 03 11:21:20 2008] [error] [client 172.18.200.62]
>>         Premature end of script headers: lista_alarmes.cgi, referer:
>>         http://172.18.200.153/cgi-bin/get_ret.cgi
>>
>>         I can only think that Apache doesn't know how to execute
>>         SQLDriverConnect. But if this is true,how do I make it work?
>>
>>         Any help would be very appreciated. I have no more clues or
>>         ideas to solve this right now...
>>
>>         Best regards,
>>
>>         Miguel Rentes
>>
>>
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.unixodbc.org/pipermail/unixodbc-support/attachments/20080603/b31141ee/attachment.html>


More information about the unixODBC-support mailing list