from the DBI perldoc:

           The "fetchall_arrayref" method can be used to fetch
           all the data to be returned from a prepared and exe-
           cuted statement handle. It returns a reference to an
           array that contains one reference per row.

so:
$res = $sth->fetchall_arrayref;

and then you can do a foreach in this way:

foreach $row (@$res) {
        $hostname = $row->[0];

..... do stuff with $hostname ....

basically, the hostname is the first item in the array referenced by
$row. So instead of foreaching the array of hostnames, you foreach the
array of references to a 1-item array of hostnames.

Since you're going to loop on foreach, don't loop twice. (which is what
DBIx::Simple would probably do behind the scenes).

H.


On Mon, 7 Oct 2002, Janek Schleicher wrote:

> Chad Kellerman wrote:
> 
> > Hi everyone,
> > 
> >    I thought $sth->fetchrow_array would do the trick but its not.  I am
> > using mysql on linux and I have a query:
> > 
> > SELECT hostname FROM tblHost WHERE status="started";
> > 
> > which returns a list of hostnames.  I though the fetchrow_array would
> > return a array of hostnames but it does not seem to.  It seems to only
> > return the first hostname unless I put it in a while loop.
> > 
> >   Is there a way that I can grab every hostname from the table into an
> > array so that I can run soemthing like a foreach on it??
> 
> If you can live with a little speed penalty,
> you can use the DBIx::Simple module from CPAN.
> 
> use DBIx::Simple;
> my $db = DBIx::Simple->connect('DBI:mysql:$db', $user, $passwd)
>      or die "Cannot connect to database: $!";
> my @hostname = $db->query(
>     qq{SELECT hostname FROM tblHost WHERE status = ?},
>     'started'
> )->flat;
> foreach (@hostname) {
>     # ...
> }
> 
> 
> Greetings,
> Janek
> 

---
Henri Asseily
[EMAIL PROTECTED]

Chief Technology Officer
BizRate.com

"Que les inop�rants laissent passer ceux qui font"
                               Achille Talon

Reply via email to