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

Hello:

I'm still working on my Database search engine. I got my text based search engine working just fine.

However, my CGI transformation doesn't work at all.

Here is the CGI script. There isn't too much to it yet. I just want it to print out all of my related files.

#!/usr/bin/perl -wT use strict; use Fcntl; use DBI; use CGI; use File::Basename; use Text::English; #input Words my ($q, $query, $type); my (@words); $q = new CGI; $query = $q->param("query"); $type = $q->param("type"); @words = split(/\s*(?:,|\s+)/, $query); my %paths = search(\@words, $type); for my $val (keys %paths) { print $q->a( { -href => "$val" }, "$val" ), $q->br; } sub search { my($words, $type) = @_; my($dbh, $match, $sth1, $sth2, $rule_append, $word, $file_inde +x,); my %matches; my(@poss, @lists); #Database Connection: $dbh = DBI->connect( "DBI:mysql:host=localhost;database=testd +b", "testuser", "testpass", {PrintError=>0,RaiseError= +>1}); #Establish Rules for file type: CASE: { if($type eq "email") { $rule_append = "filetype = \"email\" "; last CASE; } if($type eq "article") { $rule_append = "filetype = \"article\" "; last CASE; } $rule_append = "filetype = \"article\" OR \"email\" " +; }#End of Rule foreach $word(@$words) { my $match; ( $word ) = Text::English::stem( $word ); my @poss; #round 1: Find all file pertaining to word. my $statement1 = "SELECT files FROM catalog WHERE word + LIKE \"%$word%\" "; $sth1 = $dbh -> prepare($statement1); $sth1 -> execute(); while(my @val = $sth1 -> fetchrow_array()) { $match = $val[0]; #Get List of files. push(@lists, $match); } #created list }#End of word search loop #Parse Files and Check for matches within. foreach $match(@lists) { @poss = split(/:/, $match); foreach $file_index(@poss) { $sth2 = $dbh ->prepare("select filename, filetype from + library where filename = '\$file_index +\' and ".$rule_append); $sth2 -> execute(); my @val2 = $sth2->fetchrow_arrayref(); #print"$@val2 $\n"; $matches{$file_index} = $type; } # All Relevant Files found. }# Cycle through all possible matches. $dbh -> disconnect(); return(%matches); }#End of search Function

Here is the error

[Sun Oct 30 11:15:33 2005] [error] [client 69.119.177.200] Premature e +nd of script headers: /www/cgi-bin/searchfunc.pl

Any Ideas

Replies are listed 'Best First'.
Re: Trying to Debug DB Search: Part II
by Anonymous Monk on Oct 30, 2005 at 19:57 UTC
Re: Trying to Debug DB Search: Part II
by Cappadonna3030 (Sexton) on Oct 30, 2005 at 20:16 UTC

    The Wonders of Hacking away.... Here is the new code. Note there are some subtle changes to it

    #!/usr/bin/perl -w use strict; use Fcntl; use DBI; use CGI; use File::Basename; use Text::English; #input Words my ($q, $query, $type); my(@words); $q = new CGI; $query = $q->param("query"); $type = $q->param("type"); @words = split(/\s*(?:,|\s+)/, $query); my %paths = search(\@words, $type); print $q->header, $q->start_html( "Inverted Index Search" ), $q->h1( "Search for:".$query ); unless ( %paths ) { print $q->h2( $q->font( { -color => "#FF000" }, "No Matches Found" ) ); } for my $val (keys %paths) { print $q->a( { -href => "$val" }, "$val" ), $q->br; } sub search { my($words, $type) = @_; my($dbh, $match, $sth1, $sth2, $rule_append, $word, $file_inde +x,); my %matches; my(@poss, @lists); #Database Connection: $dbh = DBI->connect( "DBI:mysql:host=localhost;database=testd +b", "testuser", "testpass", {PrintError=>0,RaiseError= +>1}); #Establish Rules for file type: CASE: { if($type eq "email") { $rule_append = "filetype = \"email\" "; last CASE; } if($type eq "article") { $rule_append = "filetype = \"article\" "; last CASE; } $rule_append = "filetype = \"article\" OR \"email\" " +; }#End of Rule foreach $word(@$words) { my $match; ( $word ) = Text::English::stem( $word ); my @poss; #round 1: Find all file pertaining to word. my $statement1 = "SELECT files FROM catalog WHERE word + LIKE \"%$word%\" "; $sth1 = $dbh -> prepare($statement1); $sth1 -> execute(); while(my @val = $sth1 -> fetchrow_array()) { $match = $val[0]; #Get List of files. push(@lists, $match); } #created list }#End of word search loop #Parse Files and Check for matches within. foreach $match(@lists) { @poss = split(/:/, $match); foreach $file_index(@poss) { $sth2 = $dbh ->prepare("select filename, filetype from + library where filename = '\$file_index +\' and ".$rule_append); $sth2 -> execute(); my @val2 = $sth2->fetchrow_arrayref(); #print"$@val2 $\n"; $matches{$file_index} = $type; } # All Relevant Files found. }# Cycle through all possible matches. $dbh -> disconnect(); return(%matches); }#End of search Function sub to_uri_path { my $path = shift; my( $name, @elements ); do { ( $name, $path ) = fileparse( $path ); unshift @elements, $name; chop $path; } while $path; return join '/', @elements; }

    So, its almost ready to go. I just need to develop a means display the files as URLs.

    - Cappa
      That's swell. So you fixed your problem on your own, and:
      • You never told us what the real problem was -- you didn't clarify what you meant by "doesn't work at all", except to show the generic failure message that appears whenever a cgi script dies before it finishes sending output to the http client.
      • You never gave any hint whether you knew enough to check the http server's error log, to see what sort of failure message your initial script was generating, let alone tell us what was in that failure message.
      • You posted a repaired version of the code (I guessing this succeeds in a complete HTML stream for the client?), and you tell us there are subtle differences relative to the OP... Since you don't comment the changes, I guess I'll just take your word for it, because trying to diff the two versions myself is more work than I want to do.
      • You say there is more work to do -- something about displaying files as URLs -- but... well, you're printing the hash keys returned from your search as href's... Those aren't the "files"? They are the files, but the hrefs aren't working? I don't know what you mean.

      Anyway, good luck with that. If you want us to help, try to help us understand a little better.