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. ;-)
Quote Reply
Re: [Robert] Pass to SQL? In reply to
Hi,

How about this?

Code:
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'],

foreach my $tmp (@atts2) {
$fields .= qq{$tmp,);
}

GT::SQL::Condition->new ('ID','IN', [$fields]
) );
while (my ($id,$title) = $sth->fetchrow_array) {
$output .= "$id";
}
if ($output eq '') {$output = "nothing";}
return { cards => "$output", cnt => $cnt};
}

Not really sure I understand what your trying to do though :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!