sub runquery { my ($dbh, $query, $conds) = @_; my @fields = keys %$conds; my @values = (); if ( @fields ) { $query .= " where "; $query .= join " and ", map { "$_ $$conds{$_}{op} ?" } @fields; @values = map { $$conds{$_}{val} } @fields; } my $sth = $dbh->prepare( $query ); $sth->execute( @values ); my $arref = $sth->fetchrow_arrayref; # (use your own favorite here) $sth->finish; return $arref; } # that sub would be used as follows: my %qryconditions; # to be set via GUI or CGI (or not) # e.g. # = ( name => { op => '=', val => 'smith' }, # salary => { op => '>', val => 50000 }, # department => { op => 'not like', val => 'eng%' } ) # ... connect to DB (with $dbh as object handle), then: # maybe there's a UI component to choose or create this as well: my $qstr = "select name, salary, deparment from employees"; my $result_arref = runquery( $dbh, $qstr, \%qryconditions ); for my $row ( @$result_arref ) { print join( " ", @$row ), "\n"; }