I appreciate this might be more of an SQL question than a perl one, but I'm hoping I've missed something.
I have a table which looks like this:
CREATE TABLE articles ( id int(11) NOT NULL default '0', title varchar(65) NOT NULL default '', bodytext text NOT NULL, PRIMARY KEY (id) ) TYPE=MyISAM;
Now I wish to allow users to search these objects in a "typical" fashion.
The naive approach I started with, using DBI, was:
my @terms = split( / /, $search_terms ); my $querystr = "select id,title,bodytext from articles where "; my $count = 0; foreach my $term ( @terms ) { if ( $count ) { $querystr .= " or "; } $querystr .= "bodytext like '%$term%' "; $count++; } $querystr .= "order by id desc"; # Do the execution my $query = $db->prepare( $querystr ); $query->execute();
This builds up a query string like "select ... from articles where text like %a% or text like %b% or text like %c%".
However this is bad code, it doesnt use parameters like most of my DBI code, and it doesnt allow flexiability. I realise I wish the user to be allowed to search:
My SQL-fu is weak, and I see nothing obvious in the DBI docs.
Any assistance is welcome, although I should point out this is just a small part of an application which is working (and designed) pretty well - so I think switching to Class::Dbi is unlikely.
In reply to OT - Searching databases effectively by skx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |