Forum Moderators: coopster & phranque

Message Too Old, No Replies

Dynamic Query Generation

Constructing the query as per the requirements

         

shaan1980

2:38 pm on Jun 8, 2004 (gmt 0)

10+ Year Member



I am a novice at perl and Have a quick question.
i want to add on to the query as per what data I have
eg:

query=select ID where student='Roger';

If $address>5
then query.AND Address=$address

something like that

Please help
Thanks

VectorJ

3:37 pm on Jun 8, 2004 (gmt 0)

10+ Year Member



Here's your pseudocode translated into Perl:


$query = "select ID where student='Roger'";
if ($address > 5) {
$query .= "and Address='$address'";
}

You'll also probably need to use the DBI module to access your database. If you're using mysql:


#first connect to the database
$c_dsn = 'DBI:mysql:database=' . $database;
$c_dsn = $c_dsn . ';host=' . $host;
$c_dsn = $c_dsn . ';port=' . $port;
$db_conn = DBI->connect($c_dsn, $username, $password);
#now execute the query
$executing = $db_conn->prepare($query);
$db_ref = $executing->execute();
#read information
while (@res = $db_ref->fetchrow_array) {
#do something with the result array
for $res(@res) {
print $res . "\n";
}
}

Hope this helps.

SeanW

12:46 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



[edit]Need more coffee. Should have read closer. Never mind[/edit]