Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling


by rb_rb (Initiate)
on May 27, 2005 at 10:48 UTC ( #461017=perlquestion: print w/replies, xml ) Need Help??

rb_rb has asked for the wisdom of the Perl Monks concerning the following question:

Hallo, I want to get out a list of values out of my database. I tried out the method fetchrow_array and expected to receive an array. But I only got the first matching value, not all. What can be the mistake? And this is the code:
$sql = "select status from sh_protokoll where asek_jn = 'j'"; $sth = $dbh->prepare($sql); $res = $sth->execute(); @ary = $sth->fetchrow_array(); $res = $sth->finish();
Many thanks R.B.

Replies are listed 'Best First'.
Re: fetchrow_array
by marto (Cardinal) on May 27, 2005 at 10:55 UTC

    This method fetches the next row of data and returns it as an array of field values.

    while(@row = $sth->fetchrow_array) { print qw($row[0]\t$row[1]\t$row[2]\n); }

    From this page.
    Hope this helps.


    Janitored by holli - fixed formatting

Re: fetchrow_array
by monkey_boy (Priest) on May 27, 2005 at 11:09 UTC
    there will only ever be one value in the array when you select one value from the database, i think you are confusing rows with columns, try:
    my @ary = @{$dbh->selectcol_arrayref("select status from sh_protokoll +where asek_jn = 'j'")};

    This is not a Signature...
Re: fetchrow_array
by bradcathey (Prior) on May 27, 2005 at 11:22 UTC

    Or to grab all the rows in column-value pairs into a hash ref (handy for HTML::Template),try:

    $ary = $sth->fetchall_arrayref({}); for my $i ( 0 .. $#$ary ) { print $ary->[$i]{'status'}; }

    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

      Alternatively you can use bind_columns to do this (except row by row instead of all at once). From the docs (prepare something like "select region, sales from table"):

      $sth->execute; my %row; $sth->bind_columns( \( @row{ @{$sth->{NAME_lc} } } )); while ($sth->fetch) { print "$row{region}: $row{sales}\n"; }

      Updated: Added better reference to documentation

      "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

        Thanks, but you lost me on:

        $sth->bind_columns( \( @row{ @{$sth->{NAME_lc} } } ));

        Can you unpack that for me? It thought binding is related to placeholders. What exactly is bind_columns doing and what is the gain? Appreciate it, TIA!

        "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://461017]
Approved by Joost
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2021-12-02 19:23 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (23 votes). Check out past polls.