foobarbaz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Hope someone can help with this. I'm running perl on w2k for the first time and my cgi script within a form always fails at the DBI->connect(). Yet the same script executed standalone "perl sriptname.cgi" on w2k works fine. I can connect using Sql*Plus from my w2k box so I know that the connection works. I'm using the following: DBD-ODBC v1.05 DBI v1.37 Apache_1.3.28-win32-x86 Activestate perl 5.6.1 Oracle73Ver2.5 driver W2k (sp4) Can anyone offer advice? Thanks

Replies are listed 'Best First'.
Re: perl db connection problems
by edan (Curate) on Sep 15, 2003 at 14:23 UTC

    Problems of this sort are often related to the different environment that your web server runs the script from, vs. you the user. In particular, with Oracle, things like $ORACLE_HOME need to be set for things to work properly. Perhaps you need to set that in your script, say, in a BEGIN {} block?

    Just a thought

    Update: On further thought, chances are that ODBC takes care of things like that for you - that's definitely an issue when you use DBD::Oracle. I just glanced through the documentation for DBD::ODBC, and I couldn't help but notice a heading entitled 'Using DBD::ODBC with web servers under Win32.' Perhaps the info there will be of help to you?

    --
    3dan
Re: perl db connection problems
by dorko (Prior) on Sep 15, 2003 at 14:28 UTC
    Whenever someone says "it works fine from the command line but it doesn't work as CGI..." it makes me think of users' permissions. The user executing CGI scripts is oftentimes "apache" or "nobody". When you run the same script from the command line, you're logged in as a user with rights and permissions that are usually quite different from apache/nobody.

    It may or may not help, but it's something to think about.

    Cheers,

    Brent
    -- Yeah, I'm a Delt.

Re: perl db connection problems
by tachyon (Chancellor) on Sep 15, 2003 at 14:18 UTC

    If you have isolated the problem to the connect it would make sense to see what DBI says don't ya think?

    my $dbh = DBI->connect("dbi:$db_type:$db_name", $db_username, $db_pass +word, @_ ) or confess( "Could not connect to database\n" . $DBI::errstr ); sub confess { print "Content-Type: text/html\n\n" . shift }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Yep, did that. The errstr simply says: DBI connect('Oracle7','***',...) failed: line 18

        It would help to show some code and the exact error message (by all means s/real_username/username/ s/real_password/password/ s/real_ip/x.x.x.x/ etc). The actual error message in all its exacting detail is typically relevant. Do you actually get '***', .... ? When a script runs on the command line (as user you presumbably, IP ? localhost) you need to understand that under CGI it probably runs as user ? apache ? nobody, IP ? real_ip. In other words the environment is different. It seems as though you are connecting to a remote DB which will have in it a remote access perms table that might look like:

        user perms you@somewhere * you@% *

        That's fine when you try to connect but not so good when apache@perhaps_some_other_ip tries to connect. If that is not the problem you also possibly have a firewall between you and the remote DB. DBs connect on unusual ports, and you can punch holes through firewalls on a global or per user basis.....

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: perl db connection problems
by Grygonos (Chaplain) on Sep 15, 2003 at 14:26 UTC

    using my $dbh = DBI->connect(x,x,x) or die "$!"; would be a good start. This would tell you what its "barfing" on.

    Just a thought.. hope it helps.

    Had a break between post start and post complete...sorry.