[ic] Array from database

Mike Heins mikeh@minivend.com
Tue, 19 Dec 2000 13:03:01 -0500

Quoting Kaare Rasmussen (kar@webline.dk):
> I'd like to fill an array with the values of my order lines. After messing 
> around for some time, I'm in doubt.
> Seems to be best to do in perl. But all options seem a bit flawed, so which 
> is best?
> - $tag->data can only handle one row (??)
> - %db may be better, but the key has to be named "code" (??)
> - %sql gives you all the power, but is more complex.
> - More?

Why do you say that $Db{table} is not it?

	[perl tables=products]
		my $db = $Db{products} 
			or return "NO products?";
		my $q = "select foo, bar from products where buz = 'baz'";
		my $ary = $db->query($q)
			or die "query failed";
		foreach $row (@$ary) {
			($foo, $bar) = @$row;
			## Your code here

If you want to get back a hash with name pointers and an array with field
names, you can call it in array context:

	my ($ary, $name_hash, $name_ary) = $db->query($q)
		or die "query failed";
	$out .= "index of foo is $name_hash{foo}\n";
	$out .= "field name at index 1 is $name_ary[1]\n";

	my $rc = scalar @$ary;
	$out .= "Query returned $rc rows";

Didn't I document this? I thought I did.

Akopia, Inc., 131 Willow Lane, Floor 2, Oxford, OH  45056
phone +1.513.523.7621 fax 7501 <heins@akopia.com>

Function in chaos, finish in style. -- Unknown