469,951 Members | 2,352 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

DBI Help

Hello

I was told that for my Perl/CGI assignment to use a foreach loop, something
like:

foreach $row (sth->fetchrow_array) {

}
(where sth is the sql handler that was used to query the database)

Now what I want to do is add all of the values returned by the query to an
array.

Any suggestions?
Jul 19 '05 #1
1 2109
"Brett Baisley" <ba*****@hotmail.com.REMOVETHIS> wrote in message news:<r5**********************@ursa-nb00s0.nbnet.nb.ca>...
I was told that for my Perl/CGI assignment to use a foreach loop,
Why? Were you given any other handicaps?
foreach $row (sth->fetchrow_array) {

}
There are several mistakes there.

$row is iterating over the colums of a single row. I doubt that is what you want.

More likely you mean is:

while ( my @row = $sth->fetchrow_array) {
# do stuff with @row
}

OR

while ( my $row = $sth->fetchrow_arrayref) {
# do stuff with @$row
}

OR

while ( my ($this,$that,$the_other) = $sth->fetchrow_array) {
# do stuff with $this,$that,$the_other
}
Now what I want to do is add all of the values returned by the query to an
array.
The Perl function to add to an array is push().

See also the selectcol_arrayref, fetchall_arrayref and selectall_arrayref methods.

The fetchcol_array and fetchcol_arrayref methods are conspicuous by their absense!
Any suggestions?


Read the DBI documentation.

Never again listen to the person who suggested a for loop.

This newsgroup does not exist (see FAQ). Do not start threads here.
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tom | last post: by
6 posts views Thread by wukexin | last post: by
3 posts views Thread by Colin J. Williams | last post: by
7 posts views Thread by Corepaul | last post: by
5 posts views Thread by Steve | last post: by
8 posts views Thread by Mark | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.