in reply to 3 strings to join
Usually the push'es are in a block where the variables are being created, so I don't need the 'if $b' or 'if $c' clauses.my $sql="$a where "; my @where; push @where, $b if $b; push @where, $c if $c; $sql .= join " and ", @where;
Some of the many benefits of placeholders are that you don't have to escape quotes (or generally any other character), you can prepare the statement once and execute it many times with different arguments, and even if you DO only execute it once in this script, some databases can cache the statement and so using placeholders can let the database utilize the cache more efficiently over multiple executions with different arguments.my $sql="$a where "; my (@where, @args); if ($something) { push @where, "field1 = ?"; push @args, $some_value; } if ($something_else) { push @where, "field2 = ?"; push @args, $another_value; } $sql .= join " and ", @where; # Assume RaiseError is true; # If this is executed more than once # in this script, # use 'prepare_cached' instead. my $sth = $dbh->prepare($sql); $sth->execute(@args);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: 3 strings to join
by jptxs (Curate) on Sep 29, 2000 at 02:27 UTC | |
by runrig (Abbot) on Sep 29, 2000 at 02:34 UTC |