[ back to toc ]

sql and perl

Date: 2002/05/31 17:19

Q:
Hello,
Currently I'm doing CGI/Perl application with oracle9i database. I want to
do a database query
based on user selection. My question is as follows:
#each value comes from a single select scrolling #list
my $category=param('categorysel');
my $specie=param('speciesel');
my $author=param('authorsel');
if($category)
{$clause="(1=1) and category='$category'";}
if($specie)
{$clause="(1=1) and specie='$specie'";}
if($author)
{$clause="(1=1) and author='$author'";}

$statement=qq(select * from genlab.general_information where '$clause');
$sth=$dbh->prepare($statement);
$sth->execute;

I want to use a variable to get the various conditions based on user
selection. But it
seems my method doesn't work.

I shall appreciate very much for your help!
have a nice day!
*NAME-DELETED*

A:
You have the whole 'where clause' as a string. You have to change:

$statement=qq(select * from genlab.general_information where '$clause');

to

$statement=qq(select * from genlab.general_information where $clause);

I also do not understands why each of the clauses contain (1=1) and?

You may wanted to write:

$clause = "(1=1)"
if($category)
{$clause .=" and category='$category'";}
if($specie)
{$clause .=" and specie='$specie'";}
if($author)
{$clause .=" and author='$author'";}

Regards,
Peter

[ back to toc ]