Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: if/else options

by dsheroh (Monsignor)
on Aug 21, 2009 at 11:41 UTC ( #790335=note: print w/replies, xml ) Need Help??


in reply to Re: if/else options
in thread if/else options

Except, of course, that interpolating user-supplied input directly into your SQL statements is bad ju-ju. Much better would be:
my @where; my @values; my $sql = 'select * from table'; if defined $var1 { push @where, 'column1 = ?'; push @values, $var1; } if defined $var2 { push @where, 'column2 = ?'; push @values, $var2; } if defined $var3 { push @where, 'column3 = ?'; push @values, $var3; } $sql = $sql . ' where ' . join(' and ', @where); my $sth = $dbh->prepare_cached($sql); $sth->execute(@values);
or
my @values = (undef, $var1, $var2, $var3); my @where; my $sql = 'select * from table'; my @defined_values; foreach my idx (1..3) { if defined $values[$idx] { push @where, qq(column$dx = ?); push @defined_values, $values[$idx]; } } $sql = $sql . ' where ' . join(' and ', @where); my $sth = $dbh->prepare_cached($sql); $sth->execute(@defined_values);

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2020-10-27 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (259 votes). Check out past polls.

    Notices?