Just change your control flow slightly and prepare/execute the entire statement inside the if/elsif/else blocks (warning: untested code)
sub get_musings_entries {
# get musings from MySQL
my ($musing_id,$ref_id) = @_;
if($ref_id && $ref_id eq 'all') {
$sth = $dbh->prepare(qq|SELECT musing_id, ref_id,
heading,abstract,
DATE_FORMAT(entry_date, '%M %e, %Y')
FROM musings
ORDER BY entry_date DESC|);
$sth->execute();
}
elsif($ref_id) {
$sth = $dbh->prepare(qq|SELECT musing_id, ref_id,
heading, abstract,
DATE_FORMAT(entry_date, '%M %e, %Y')
FROM musings
WHERE ref_id = '?'
ORDER BY entry_date DESC
LIMIT 10|);
$sth->execute($ref_id);
}
else {
$sth = $dbh->prepare(qq|SELECT musing_id, ref_id,
heading, entry,
DATE_FORMAT(entry_date, '%M %e, %Y')
FROM musings
WHERE musing_id = '?'|);
$sth->execute($musing_id);
}
my $entries = $dbh->selectall_arrayref($sql_stmnt);
$sth->finish();
return $entries;
}
|