in reply to Varying numbers of DBI placeholders
As has already been pointed out, you can not do that. But in the code you provided, you would not want to. Consider the case when $item2 is undef and $item3 is defined, you'd be selecting from foo where item3 = ''. Not what you want to do.
my (@where, @params ); push ( @where, 'item1 = ?' ); push ( @params, $item1 ); if ( defined( $item2 ) ) { push( @where, 'item2 = ?' ); push( @params, $item2 ); } if ( defined( $item3 ) ) { push( @where, 'item3 = ?' ); push( @params, $item3 ); } my $script = sprintf("SELECT foo FROM bar WHERE %s", join ( ' AND ', @WHERE ) ); ... $sql->execute( @params );
This code is a bit more verbose than necessary. It could be cleaned up by using a configuration hash and looping through each of the items you are searching on rather than doing them linearly, but I think its good enough to demonstrate one way of addressing the issue.
|
|---|