Filling in the missing pieces in the approach suggested by Gangabass above:
#!/usr/bin/perl use strict; use DBI; my $dbh = DBI->connect( ... ); my @val1_set = qw/foo bar baz quz/; my $sth = $dbh->prepare("select val1,val2,val3 from atable where val1= +?"); my @result_set; for my $val ( @val1_set ) { $sth->execute( $val ); my $result = $sth->fetchall_arrayref; push @result_set, $result; } for my $i ( 0 .. $#val1_set ) { print "Results for val1=$val1_set[$i]:\n"; if ( ref( $result_set[$i] ) ne "ARRAY" or @{$result_set[$i]} == 0 ) { print "__EMPTY_SET__\n\n" } else { for my $row ( @{$result_set[$i]} ) { print join("\t", @$row ),"\n" } print "\n"; } }
That will work no matter how many rows get returned (0, 1 or more) on each iteration over the set of "val1" values. For any non-empty return from the execute() call, the "fetchall_arrayref" always returns a reference to an AoA (a set of 1 or more rows, with each row containing a set of 1 or more columns or fields), so you have to dereference the set of rows, and then for each row, dereference the set of columns.

(update: And in this case, since the result set is an array that is built up over some number of execute calls, you are starting with an AoAoA, so the sample code uses $i to iterate over the top-level array.)


In reply to Re: Simple DBI question, select rows based on an array by graff
in thread Simple DBI question, select rows based on an array by novosirj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.