Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

CGI and DBI

by howarda (Acolyte)
on Nov 29, 2004 at 12:15 UTC ( [id://410902]=perlquestion: print w/replies, xml ) Need Help??

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

Hello I am trying to write my first perl script using CGI, DBI and DBD::ODBC to connect to an Oracle database on my Windows XP pc.
I have got simple CGI scripts working on here but this is the first trying to connect to a database.

Each time it runs I get CGI Timeout in the browser and a 502 error in the IIS log.

11:41:04 127.0.0.1 GET /CGI/ah1.cgi 502

I am able to run the database connection code in a stand alone script which works fine (even returns results) and the CGI script will run if the database connection code is comented out.

My code is below and all I want at the moment is for the database connection to suceed and display a simple page telling me so

#!C:\Perl\bin\perl.exe -wT use strict; use DBI; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/; my $tainted_mpan = param( 'mpan' ) || ''; my $tainted_date = param( 'date' ) || ''; my $mpan = ''; my $dd=''; my $mm=''; my $yy=''; my $message = 'The MPAN you have entered is not valid. Please re-enter +'; my $message1="Invalid MPAN"; my $message2="Invalid Date"; #check that the mpan is in the correct format #it may not exist but it has to be 13 numbers if ( $tainted_mpan =~ /^(\d{13})$/ ) { $mpan = $1; #display_page($message2); }else{ display_page( $message1 ); exit; } #check that the date format is correct DD/MM/YY if ( $tainted_date =~ /^(\d{2})\/(\d{2})\/(\d{2})/) { $dd=$1; $mm=$2; $yy=$3; search_mpan(); }else{ display_page($message2); exit; } #routine to display any messages generated sub display_page { my $message = shift; print header, start_html, p( $message ), end_html; } sub search_mpan { my @rows; my $dbh = DBI->connect('dbi:ODBC:database.server',"username", "pas +sword") or display_page("$DBI::errstr"); display_page("This worked"); }

Thanks for any help regarding this.
howarda

Replies are listed 'Best First'.
Re: CGI and DBI
by hmerrill (Friar) on Nov 29, 2004 at 12:29 UTC
    Forget about connecting to the database for the moment, and first make sure that you can get your cgi script to run in the browser. Try something like this first:
    #!C:\Perl\bin\perl.exe -wT use strict; use DBI; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/; display_page("Got here!"); #routine to display any messages generated sub display_page { my $message = shift; print header, start_html, p( $message ), end_html; }
    If that works in the browser, *then* move on to trying to get the database connection working.
Re: CGI and DBI
by castaway (Parson) on Nov 29, 2004 at 13:17 UTC
    If you're getting timeouts, maybe it just takes too long? Try running the script from the command line, to see if it actually works at all?

    C.

      Hi, I can run the script from the command line which runs successfully after about 4 seconds.
      I thought the issue was with my personal firewall but I have switched that off now and that has made no difference.
        Did you do as I suggested and trim your script down to just display a message and NOT do the database stuff? Doing this will reveal whether your problem is webserver permissions related. Get your bare-bones cgi script (that just displays a message) to work in the browser first, and *THEN* try adding in the database stuff.
Re: CGI and DBI
by htoug (Deacon) on Nov 29, 2004 at 13:09 UTC
    Connecting to a database is something that takes time - sometimes *very* long time, and so adding that to the normal 'sluggishness' of CGI invocation might give you something that is unacceptably slow.

    YMMV.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://410902]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found