DBI / mySQL / IIS6 = "The specified CGI application misbehaved by not returning a complete set of HTTP headers."

I have a Perl CGI script that fails to function, here's the HTML FORM that submits to the script:

  <TABLE BORDER=1 width="441">
  <TH width="464">Name Search</TH>
  <TR><TD>
  <FORM METHOD=POST ACTION="http://myhost.local/cgi-bin/a_search.cgi">
  <P>Enter Name:
  <INPUT NAME="na" TYPE=TEXT size="30" maxlength="30">
  <P>Enter County:
  <INPUT NAME="co" TYPE=TEXT size="30" maxlength="30">
  <P>Enter Year:
  <INPUT NAME="yr" TYPE=TEXT size="4" maxlength="4">
  <BR><BR>
  <INPUT TYPE=SUBMIT VALUE="Search Name Index">
  <INPUT TYPE=RESET>
  </FORM>
  </TD>
  </TR>
  </Table>

The form submits to the script fine, but then from the script I receive a "CGI Error" "The specified CGI application misbehaved by not returning a complete set of HTTP headers."  No additional information is given.

I am attempting to connect to a mySQL DB that exists on the same server as the script is running from, using ActiveState ActivePerl 5.8.  Thanks in advance for your assistance.

Here's the a_search.cgi script:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Search Results</TITLE></HEAD>\n";

use DBI;
use NIC::CGI;

$user = 'myusername';
$pass = 'mypassword';

my $req = new NIC::CGI;
$vars = $req->import_hash;

$dbh = DBI->connect('DBI:mysql:a_index, $user, $pass)
      || db_error;

foreach $key (keys %vars) {
      $vars->{$key} =~ s/[\"\']//g;
}

$query = "SELECT * FROM a_index WHERE ";

$and_query = 0;

if ($vars->{'na'} !~ /^\s*$/) {

      $query .= "Name LIKE '%$vars->{'na'}%'";
      $and_query = 1;

}

if ($vars->{'co'} !~ /^\s*$/) {

      $query .= " AND " if ($and_query);
      $query .= "County LIKE '%$vars->{'co'}%'";
      $and_query = 1;

}
      
if ($vars->{'yr'} !~ /^\s*$/) {

      $query .= " AND " if ($and_query);
      $query .= "Year LIKE '%$vars->{'yr'}%'";

}


$sth = $dbh->prepare($query)
      || die "Couldn't prepare \"$query\": $DBI::errstr\n";

$sth->execute
      || die "Couldn't execute \"$query\": $DBI::errstr\n";


print "<BODY BGCOLOR=#FFFFFF>\n";
print "<H2>Search results for Name Search</H2>\n";

print "<!-- query = $query -->\n";

print "<BR><TABLE BORDER=1>\n";
print "<TH>Name</TH>\n";
print "<TH>County</TH>\n";
print "<TH>Year</TH>\n";

while (@row = $sth->fetchrow) {

      print "<TR>";
      foreach $element (@row) {
            print "\t<TD>$element<BR></TD>\n";
      }
      print "</TR>\n";

}

print "</TABLE>\n";
print "</BODY>\n";
print "</HTML>\n";
LVL 27
ddrudikAsked:
Who is Participating?
 
Perl_DiverCommented:
You can add:

use CGI::Carp qw/fatalsToBrowser/;

below the shebang line and it might help trace down the problem.

Why is the CGI modlue being loaded like this:

use NIC::CGI;

or is that a private module you wrote? I've never header of NIC::CGI and I didn't find it anywhere.
0
 
mjcoyneCommented:
Start your script with:

use CGI;
$q = new CGI;
print $q->header();

instead of print "Content-type: text/html\n\n";
0
 
ahoffmannCommented:
what does db_error do?
Also add
  exit(0);
at the very end.

not sure how your modules work, but DBI->connect() should look like, usually:
  DBI->connect("DBI:mysql:database=a_index;host=localhost", $user, $pass);
and finally I'd use (at least for testing):
  DBI->connect("DBI:mysql:database=a_index;host=localhost", $user, $pass, {'RaiseError'=>1});
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
ahoffmannCommented:
> print $q->header();
and
> print "Content-type: text/html\n\n";
are identical, usually ..
0
 
ddrudikAuthor Commented:
Thanks for all of the great tips, I was working from a code example, I changed the use CGI line per Perl_Diver's suggestion and I get a more descriptive error:

Software error:
Bad name after na' at C:\Inetpub\nshs\cgi-bin\a_search.cgi line 21.

Here are the first 21 lines of code:
#!/usr/bin/perl
use CGI::Carp qw/fatalsToBrowser/;
my $req = new CGI;
$vars = $req->import_hash;


$user = 'myusername';
$pass = 'mypassword';

$dbh = DBI->connect('DBI:mysql:a_index, $user, $pass)
      || db_error;

foreach $key (keys %vars) {
      $vars->{$key} =~ s/[\"\']//g;
}

$query = "SELECT * FROM a_index WHERE ";

$and_query = 0;

if ($vars->{'na'} !~ /^\s*$/) {
0
 
ahoffmannCommented:
not sure if it makes a difference for your perl, but I'd change
       $query .= "Name LIKE '%$vars->{'na'}%'";
to
       $query .= "Name LIKE '%" . $vars->{'na'} . "%'";

(and all others too)
0
 
ddrudikAuthor Commented:
I was missing an apostrophe in the DBI line that was causing my last error, now I have a new error to contend with:
Software error:
Can't locate object method "new" via package "CGI::Carp" at C:\Inetpub\nshs\cgi-bin\atlas_search.cgi line 3.

As I mentioned, I am starting with other's code so I am sure there will be a number of things I might have to change, what I am interested in accomplishing is to just take the variables from the HTML FORM (if any are entered in the form) and construct a query that would be an AND of each of the variables.  I assume the my $req line is required in conjunction in with $vars= line in order to import the variables from the HTML FORM.

Here's the current start of my code:
#!/usr/bin/perl
use CGI::Carp qw/fatalsToBrowser/;
my $req = new CGI::Carp;
$vars = $req->import_hash;


$user = 'myusername';
$pass = 'mypassword';

$dbh = DBI->connect('DBI:mysql:a_index', $user, $pass)
0
 
ahoffmannCommented:
> my $req = new CGI::Carp;
I guess you mean:
  my $req = new CGI;

but if I see your used variable name, I guess that you want to do something different.

You should make yourself used to CGI and BD modules first, try&error will not give you a quick solution.
0
 
ddrudikAuthor Commented:
Same kind of error:
"Software error:
Can't locate object method "new" via package "CGI" at C:\Inetpub\nshs\cgi-bin\atlas_search.cgi line 3."

Current start of code:
#!/usr/bin/perl
use CGI::Carp qw/fatalsToBrowser/;
$req = new CGI;
$vars = $req->import_hash;

BTW, this test script works fine:
#!/usr/bin/perl
use CGI;
$q = new CGI;
print $q->header(),
      $q->start_html(-title=>'Wow!'),
      $q->h1('Wow!'),
      'Look Ma, no hands!',
      $q->end_html();

When I leave out the ::Carp qw/fatalsToBrowser/; etc in my original script it fails with the nondescript "...misbehaved..." error.

Thanks.
0
 
ahoffmannCommented:
where is your

use CGI;
0
 
ddrudikAuthor Commented:
ahoffman, please quote which code you are referring to, there is a use CGI; in the second test script and a use CGI::Carp qw/fatalsToBrowser/; in my main script.  Thanks.
0
 
ahoffmannCommented:
that code which produces the error
0
 
ddrudikAuthor Commented:
I am closing this question and opening up a different one since the problem has changed.
0
 
ahoffmannCommented:
problem has change, but how did the grded answer help you?
0
 
ddrudikAuthor Commented:
That gave me a way to at least see the error, from which I found the typo causing the initial error I was looking for.
0
 
Perl_DiverCommented:
use CGI;

and :

use CGI::Carp

are two seperate modules, you have to load both of them. So you have to do this:

#!/usr/bin/perl
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
$q = new CGI;
print $q->header(),
      $q->start_html(-title=>'Wow!'),
      $q->h1('Wow!'),
      'Look Ma, no hands!',
      $q->end_html();


After the script is working you should remove or comment out use CGI::Carp because it's really only needed to help with debugging problems.

 
0
 
ddrudikAuthor Commented:
Thanks.  I will check that out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.