CDaly CDaly - 1 month ago 9
Perl Question

Perl DBI 2 dynamic arrays in 1 query

I currently have a webpage with 2 multiselect boxes that returns 2 different strings, which will be used in my SQL queries.

I am currently only using 1 string in my queries, but wish to add another and am unsure of where to go from here.

I have the string being created into an array

@sitetemp = split ',', $siteselect;
my $params = join ', ' => ('?') x @sitetemp;


and am able to use a query with
$params


$mysql_inquire = "SELECT starttime, SUM(duration) FROM DB WHERE feature = \"$key\" and starttime >= $start and starttime <= $end and site IN ($params) group by starttime order by starttime";


$sth = $DBH->prepare($mysql_inquire);
$sth->execute(@sitetemp);


Essentially my question is how could I do the same thing, using 2 different arrays?

I assume the line
$sth->execute(@sitetemp, @otherarray);
would not work.

Answer

Your approach will work.

You can pass as many arrays into a function as you want. Arrays are just lists of values.

Consider the following example:

sub foo {
    print Dumper \@_;
}
my @a = ( 1, 2, 3 );
my @b = ( 4, 5, 6 );
foo( @a, @b, ( 7, (8), 9, ( ( (10) ) ) ) );

This will print:

$VAR1 = [
          1,
          2,
          3,
          4,
          5,
          6,
          7,
          8,
          9,
          10
        ];

When you say foo(@a, @b) it will just evaluate each array into a list. And you can combine as many lists as you want, they will always be flattened.

Comments