Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: How's My Style Now?

by Spenser (Friar)
on Feb 08, 2010 at 11:39 UTC ( [id://821995]=note: print w/replies, xml ) Need Help??


in reply to Re: How's My Style Now?
in thread How's My Style Now?

I usually do use placeholders. However, in this case one of the three possible SQL statements has no value required. How would deal with that? For the other two I can create a variable (e.g., $particular) and set it in block of the elsif and the else, to be used like so, $sth->execute($particular);. However, for the if statement, won't I get an error even if $particular is set to blank?

-Spenser

That's Spenser, with an "s" like the detective.

Replies are listed 'Best First'.
Re^3: How's My Style Now?
by chb (Deacon) on Feb 08, 2010 at 13:49 UTC
    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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://821995]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-26 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found