SPUG:using DBI::fetchrow_array(); to load another array

Chris Wilkes cwilkes-spug at ladro.com
Mon Feb 10 07:04:04 CST 2003

On Mon, Feb 10, 2003 at 03:21:18AM -0800, Ramon Hildreth wrote:
> I am using DBI to connect to my database and I am trying to load the
> contents of a call to fetchrow_array() into an array. 
> @the_array = $sth->fetchrow_array();
> gives me only the first row.
> makes sense after I looked at it, since I am using it in a scalar
> context.

But you're getting back an array of the columns of that row, nothing
scalar about that.  The operative word here is "fetchROW" ;)

> but how then would I get it to work 'array to array' so to speak.

Later on in the perldoc for DBI it has
	$ary_ref  = $sth->fetchall_arrayref;
which I think is what you're thinking about.  To get it into one huge
array you can do this:
	my @lottarows = @{$sth->fetchall_arrayref};
Which gives you an array of 1..n entries, with each entry being a 1..m
columns of your query.

Watch out as that could be a large array.  You're probably better off
with something like this:
	while (my @columns = $sth->fetchrow_array()) {
	  print Dumper(@columns);


More information about the spug-list mailing list