I’m converting a classified ad/flat file perl script to use mysql/dbi;
I have this search code and it’s not working very well. It will find Vintage_Trailers from item_category but if I include item_state it won’t find anything. I need it to find only Vintage_Trailers only in Nevada for instance. But the user may not choose a state so only Vintage_trailers should be found. Or maybe only keywords ‘Shasta’…Something ‘out of the box’ that I can modify would be excellent. Any help is appreciated.
$searchfor="$multi_input $keywords $user $item_category $item_city $item_state";
my $dsn = "DBI:mysql:$database";
my $dbh = DBI->connect($dsn, $userid, $password )
or die $DBI::errstr;
my @searchthings = split(/ /,$searchfor);
foreach $thing(@searchthings)
{
if ($thing){
$statement .= "(item_name like '%$thing%' or
item_desc like '%$thing%' or
item_desc2 like '%$thing%' or
item_category like '%$thing%' or
item_city like '%$thing%' or
item_state like '%$thing%' or
user like '%$thing%' ) ";
}
}
$sth = $dbh->prepare(qq(select * from ads where $statement)) or die $DBI::errstr;
$sth->execute();
while ((my (@rows)) = $sth->fetchrow_array)
{
$total_row_count= $sth->rows;
$database_rows = join ("\|", @rows);
push (@database_rows,$database_rows);
}
$sth->finish() or die $DBI::errstr;
$dbh->disconnect() or die $DBI::errstr;
1 Answer