Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Help with fetchall_arrayref

by fmogavero (Monk)
on May 15, 2001 at 20:17 UTC ( [id://80598]=perlquestion: print w/replies, xml ) Need Help??

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

Here is my current code
while (my $row = $sth->fetchrow_arrayref) { foreach ($row){ if (! ref($row)) { next; } print @$row, "\n"; } }
The if (! ref($row)){next;} does not work. It prints out two references:DBI::db=hash(0xwhatever) and DBI::st=hash(0xwhatever)and then the information that I want.

That is the secondary question and is probably cranial-rectal insertion on my part.

My primary question is: Can I use fetchall_arrayref to return all of the rows that match my query, and then parse out the information? And if so, how?

I have a query like

"select program, day, time from TVviewingsked where name = "Frank"
I would like to have all the info in computer memory to avoid tying up the database with a fetchrow_arrayref because from what I read the DBI information says that fetchall_arrayref returns an array of array references, one reference per row.

Am I even on the right track here?

Edit: chipmunk 2001-05-15

Replies are listed 'Best First'.
Re: Help with codefetchall_arrayref/code
by dustacio (Monk) on May 15, 2001 at 20:49 UTC
    Something like
    my @rows = $sth->fetchrow_arrayref; foreach my $row (@rows) { print "@row\n"; }
    might work for what you have in mind.
    "I ain't for it, I'm again' it. I just haven't finished yet." Abe Simpson
      I meant .... my $rows = $sth->fetchrow_arrayref; foreach my $row (@$rows) { print "@$row\n"; }
Re: Help with fetchall_arrayref
by mwp (Hermit) on May 16, 2001 at 05:46 UTC

    First question: You're close, but it looks like you're trying to loop through the recordset twice. You can keep your current semantics by doing something like this:

    while (my $row = $sth->fetchrow_arrayref) { print @$row, "\n"; }

    You don't need to verify that $row is a reference; fetchrow_arrayref will always return one. If you want to validate the data in each row, you'll have to do something like this:

    OUTER: while (my $row = $sth->fetchrow_arrayref) { INNER: foreach my $column (@$row) { next OUTER if($column eq undef); } print @$row, "\n"; }

    Which will skip any row with a blank column. (Not all blank columns, ANY blank column.)

    Second question: fetchrow and fetchall only affect how you layout your program, not how the database is called. In both circumstances, the entire recordset is loaded into memory. fetchrow retrieves one row at a time (from memory), and fetchall retrieves them all. I think! You'll have to check up on that.

Secondary Question fixed.
by fmogavero (Monk) on May 15, 2001 at 21:30 UTC
    The secondary question was fixed after craniectomy.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found