So it's possible that any of the three fields can be blank? I would do this:
Code:
my @fields;
push @fields, "field1=$field1" if ($field1);
push @fields, "field2=$field2" if ($field2);
push @fields, "field3=$field3" if ($field3);
my $statement = "select * from table";
if (@fields)
{
$statement .= " where ";
$" = " AND ";
$statement .= "@fields";
$" = " ";
}
The $" variable determines what string should be placed between concatenated array elements when an array appears in quotes. In this case, the fields are only added to the array if they were given, and then they are automatically appended to the query string, separated by " AND ".
HTH ;D
Bookmarks