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

Monks, I am new to cgi, i was working with simple cgi scripts and all worked fine, when i used dbi, i was stuck.

i have a database te, and i want to print $sno and $name in the database through cgi. when i ran thru dbi it was working fine, but with cgi it is not working.

i am getting error: file download, some files can harm your computer.......

Am i missing anything?

#!C:\perl\bin\perl use strict; use CGI; use DBI; my $q = new CGI; print $q->header("text\html"), $q->start_html(-title=>"test"); my $dbh = DBI->connect("DBI:mysql:te:localhost", {PrintError =>0, Rais +eError =>0}) ; #automatic error checking #DBI->trace(2); my $data = qq(select sl_no, name from det order by name); my $sth = $dbh->prepare($data); $sth->execute; #while ( ( $sno, $name) = $sth->fetchrow_array ) { # ### Print out a wee message.... # #print "Name is $sno\t$name\n"; #} $dbh->disconnect; print $q->p("$sno, $name"); print $q->end_html;

Replies are listed 'Best First'.
Re: dbi with cgi
by cbro (Pilgrim) on Jun 24, 2005 at 17:35 UTC
    The problem (besides the use strict mentioned previously) is your backwards header. Change "text\html" to "text/html". The way you have it setup causes both Safari and IE to download the file; Mozilla will print your end_html() output directly to the browser instead of interpreting it as HTML code. When loading your test script with IE, I was able to replicate the error you posted. Correct your header and you should be fine.

      cbro you are exactly correct. Now its working fine. Thanks for others also who suggested to correct my code.

Re: dbi with cgi
by izut (Chaplain) on Jun 24, 2005 at 16:17 UTC
    You have to declare the $sno and $name variables before assignment when using strict:
    ... $sth->execute; my ($sno, $name) = $sth->fetchrow_array; ... print $q->p("$sno, $name"); ...

    izut

      good catch.
      For the OP: (btw, it's good you have 'use strict' -- you can also add 'use warnings' for additional, well, warnings :) ) this would have been obvious by just doing perl -c script.pl in a dos prompt. Also, you can actually run it like perl script.pl and see what the actual HTML output is (or error messages).
Re: dbi with cgi
by Transient (Hermit) on Jun 24, 2005 at 16:20 UTC
    If you're getting an "error: file download, some files can harm your computer" - it sounds like your webserver is not set up to process .cgi (or whatever the extension is) using the perl interpreter... are you able to run any CGI's?
Re: dbi with cgi
by injunjoel (Priest) on Jun 24, 2005 at 16:30 UTC
    Greetings,
    Aside from reiterating the previous suggestion regarding use strict; I think this is more of a configuration issue than it is a Perl one. Your error File download, some files can harm your computer... Sounds like you are using IE as your browser, and it doesn't trust your localhost; you may need to add it to your list of trusted sites. I forget how to do that since I only use FireFox these days, speaking of which have you tried this in multiple browsers yet (IE, FireFox, Galeon, Konqueror, Lynxyeah I said it , etc)? I would try a google search of your error text and see what comes up. It is either that or your web server is not configured to run cgi scripts through Perl.

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo