in reply to Passing a value into a WHERE clause

But how can you pass a variable into the first part of the WHERE clause?

Your example very nearly gets you there. The trick is to supply the field name before the DBMS sees the query. Easy enough to do. All you need is Perl's standard variable interpolation. Try something like:

my $field = 'answer'; my $value = 42; my $str = dbh->prepare("SELECT * FROM t WHERE $field = ?"); $sth->execute($value); ...

Replies are listed 'Best First'.
Re: Re: Passing a value into a WHERE clause
by peppiv (Curate) on Jan 28, 2002 at 18:58 UTC
    Unfortunately I have tried this approach to no avail. I have tried it with almost every variation of syntax I can think of. It seems that no matter what I do, if I put a string ($field) in the first part of the WHERE clause it returns 'unknown column' even though it's defined before the DBMS sees the query.