Gossamer Forum
Quote Reply
Pass to SQL?
I need some special information and dont want to use additional fields or tables;
so i save something like

ID1|ID2|ID3 in a field "saver"

Then i try to get the IDs with:

Code:
sub {
my ($rec) = @_;
my $lids = $rec->{saver};
my ($j, @atts, @atts2);
if ($lids eq '') {
return;
} else {
@atts = split(/\|/,$lids);
my $le = $#atts;
my $cnt=0;
for ($j=0; $j<=$le; $j++) {
if (int($atts[$j]) > 0) {
$cnt++;
$atts2[$cnt]=$atts[$j];
}
}

my $output="";
my $cat_db = $DB->table ('Links');
$cat_db->select_options ("ORDER BY ID DESC", "LIMIT 5");
my $sth = $cat_db->select ( ['ID','Title'],
GT::SQL::Condition->new ('ID','IN', [ $atts2[1], $atts2[2], $atts2[3], $atts2[4] ]
) );
while (my ($id,$title) = $sth->fetchrow_array) {
$output .= "$id";
}
if ($output eq '') {$output = "nothing";}
return { cards => "$output", cnt => $cnt};
}
}

This produces something like SELECT FROM ... WHERE ID IN [ID1, ID2,ID3 ..]
but it is not very nice; if i have only ID1
the query is WHERE ID IN [ID1, , ,]
Mysql makes no problems.

But maybe i want to have someday 20 IDs, then i need WHERE ID IN [ID1, ID2,ID3 ... ID20];
i think there must be another way and tried it, but my problem is how to pass an var to

$placeholder = "[ID1,ID2]"
GT::SQL::Condition->new ('ID','IN', $placeholder ) );
that it makes the same as
GT::SQL::Condition->new ('ID','IN', [ID1,ID2] ) );

is wrong

the same, when do
$placeholder = "ID1,ID2"
GT::SQL::Condition->new ('ID','IN', [$placeholder] ) );

I tried to escape [ and , but it doesnt helps ...

Hope someone brings light in my poor life. ;-)
Subject Author Views Date
Thread Pass to SQL? Robert 1697 May 20, 2005, 10:27 AM
Post Re: [Robert] Pass to SQL?
Andy 1653 May 20, 2005, 12:16 PM