Another problem is:
my $statement1 = "SELECT files FROM catalog WHERE words LIKE %".$word. +"%"; $sth1 = $dbh -> prepare($statement1); $sth1 -> execute();
I don't think SELECT files FROM catalog WHERE words LIKE %something% is valid SQL. The %something% needs to be quoted. Which leads me to placeholders.
my $find_files = $dbh->prepare(q{ SELECT files FROM catalog WHERE words LIKE ? }); for my $w (@words) { $find_files->execute("%$w%"); while (my ($file) = $find_files->fetchrow_array) { ... } }
You run into a similar problem later:
$sth2 = $dbh ->prepare("select filename, type from library where filename = $file_index and".$rule_append);
I'm guessing $file_index is a string, not a number, which will lead you to another SQL syntax error. Placeholders are your friends. I would create a hash with the three possible SQL statements, already prepared:
my %search_library = ( email => $dbh->prepare(q{ SELECT filename, type FROM library WHERE filename = ? AND filetype = 'email' }), article => $dbh->prepare(q{ SELECT filename, type FROM library WHERE filename = ? AND filetype = 'article' }), both => $dbh->prepare(q{ SELECT filename, type FROM library WHERE filename = ? AND (filetype = 'email' OR filetype = 'article') }), );
Now you can just do:
for my $w (@words) { $find_files->execute("%$w%"); while (my ($file) = $find_files->fetchrow_array) { for my $f (split /:/, $file) { $search_library{$type}->execute($f); if (my @rec = $search_library{$type}->fetchrow_array) { $match{$rec[0]} = $rec[1]; } } } }
Another comment is that I don't know why you're fetching 'filename' from the library table when you're using it to find the columns, unless you're looking to get the normalized value of it (that is, the value as it appears in the database, since your database might be case insensitive).

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

In reply to Re^4: Trying to Debug DB Search by japhy
in thread Trying to Debug DB Search by Cappadonna3030

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.