in reply to Database Output
You want to call fetchrow_hashref once. When you’ve done this, $rec now contains a hashref containing the entire row: all of the columns mentioned in your SELECT statement are simultaneously present in it. Each time you call it, another row is fetched, until no more rows exist, whereupon it returns undef.
Another thought (caution: extemporaneous coding-sketch, stripped to the bone...)
# PREPARE THE STATEMENT ONCE; USE PLACEHOLDERS my $sth = .. prepare('select ... where field3=? ...'; foreach my $cookie_name (qw/C1 C2/) { # LIST OF NAMES my $cookie_value = $cgi->cookie($cookie_name); if (defined($cookie_value)) { # FOUND A COOKIE! $sth->execute(($cookie_value)); # BIND PARAMETERS ON-THE-FLY if (my $rec = $sth->fetchrow_hashref ...
The essential ideas here are: