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

hi monks,
I have written a smaller script in cgi with dbi but problem is that when i run this script through command prompt then i see the result what is expexted but at browser i am not been able to see the result.please help me get out of this.
Code snippet is as follows:
#!C:/Perl/bin/perl.exe use DBI; use CGI qw/:standard/; $query = new CGI; $abc = $query->param('first_name'); $dbh = DBI->connect("dbi:DB2:VOTE","","") or die "Can't connect to sample database :DBI::errstr"; $stmt = "SELECT * from VOTE"; $sth = $dbh->prepare($stmt); # $sth->bind_param(1,$abc); $sth->execute(); print "Content-Type: text/html\n\n"; print <<_HEADER_; <table> <tr> <th>emp_name</th> <th>emp_count</th> </tr> _HEADER_ $sth->bind_col(1,\$col1); $sth->bind_col(2,\$col2); while ($sth->fetch) { print "$col1"; print "$col2\n"; } print "DONE \n"; print <<_FOOTER_; </table> _FOOTER_ $sth->finish; $dbh->disconnect;

Replies are listed 'Best First'.
Re: CGI-DBI Connectivity and Output
by shmem (Chancellor) on Nov 24, 2007 at 12:03 UTC
    You don't write head/body sections. Since you use CGI, use its methods:
    # print "Content-Type: text/html\n\n"; print $query->header(), $query->start_html(); # rest of code... print $query->end_html();

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: CGI-DBI Connectivity and Output
by tachyon-II (Chaplain) on Nov 24, 2007 at 12:54 UTC
    please help me get out of this

    Why not help yourself? If your code produces the output you expect on the command line but does not work as a CGI you probably have a permissions problem, provided you remembered to output a header which you did.

    You have a die() in there when you try to connect so I suspect your script is dying there. You could easily check this by looking in the server error log, but you will probably find developing CGI code a lot easier if you add this to the very top of your code, just after the shebang line:

    # ensure all fatals go to browser during debugging and set-up # comment this BEGIN block out on production code for security BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser'); } # all the rest of the code goes here
Re: CGI-DBI Connectivity and Output
by Cubes (Pilgrim) on Nov 24, 2007 at 13:30 UTC
    You could also check your web server's error log to see what sort of messages your script might be generating -- this will let you know if you have a permission problem, a code problem, or some other type of error.

    If you don't know where to find the error log, ask your web server administrator -- the file's location will vary from one system to the next.

      Thanks for your suggestion guys.The problem has been resolved.When i saw the error log then i saw that i had not used MS.VOTE in my query. Due to that call to DB2 was failing .
      But i want to ask one more query here that why there was problem with browser and not with command prompt.
        Hard to say without more information.

        My first guess would be that you have some database-related variables set in your environment that are used when you're running it from the command line, but your web server / CGI environment is different.