[ic] How do I pull out the field-names from a query?

Mike Heins interchange-users@icdevgroup.org
Tue Jul 9 13:57:01 2002


Quoting Rene Hertell (interchange@hertell.com):
> > Examine the calling documentation better.
> > 
> > 	($ary, $field_hash, $name_array) = $db->query($sql);
> 
> Hi Mike,
> 
> I'm sorry, but I can't find that part of the documentation.. :(
> 
> Could you please guide me in the right direction..
> 

The right direction is to give me a swat upside the head. Or the person
who mangled my embedded Perl documentation, anyway. 8-)

Which could have been me, I suppose. I am sure I had it in there, but it
got lost as so many things did during the docs changeover. 8-\

There is a missing call in ictemplates.sdf:

	## $ary is result array of arrays/hashes
	## $field_hash gives pointers to field indicies in the $ary
	## $name_array is field names for query
 	($ary, $field_hash, $name_array) = $db->query($sql);

So if you make this call:

	my $sql = 'select sku,price,description from products';
 	my ($ary, $fh, $na) = $db->query($sql);

$na will be [ 'sku, price, description' ].

Perhaps more importantly, $fh will be

	{ sku => 0, price => 1, description => 2 }

so you can reference fields by:

	for my $record (@$ary) {
		my $desc = $record->[$fh->{description}];
	}
        
-- 
Mike Heins
Perusion -- Expert Interchange Consulting    http://www.perusion.com/
phone +1.513.523.7621      <mike@perusion.com>

Fast, reliable, cheap.  Pick two and we'll talk.  -- unknown