in reply to getting the columns names from a mysql query

I'm wondering if you are approaching this the right way. Normally I'm stuffing these $row's into an array and then dereferencing them when I need it.

I usually end up writing something more like:
completely untested sample code follows

while (my $row = $sth->fetchrow_hashref) { push(@profiles,$row); }

then you deref @profiles as you go

foreach (@profiles) { print "$_->{user_id}\n"; }

A couple of other things I noticed

$storedProfile = {};
does not do what you think it does. It creates a scalar called $storedProfile and assigns to it an empty hashref. It looks like you are not using strict, this would not compile if you were using strict (assuming you wanted an empty hash called %storedProfile). BTW to clear a hash you want to '%storedProfile = ()'

also you can rewrite this

@keys = keys %row; for (@keys){

as

foreach (keys %row) {

Which IMO looks cleaner.



grep
Unix - where you can thrown the manual on the keyboard and get a command

Replies are listed 'Best First'.
Re: Re: getting the columns names from a mysql query
by Anonymous Monk on May 02, 2002 at 05:08 UTC
    BTW to clear a hash you want to '%storedProfile = ()'
    actually -- thats what i have in my code.. the $ was a sloppy typo in the browser :)
    while (my $row = $sth->fetchrow_hashref)
    i do that when i'm expecting more than one row -- but in this case, i'm searching by a unique key, so there can only be one row -- the while loop isn't necessary :) thanks to all.. i'm gonna test this all..
    %storedProfile=%{$query->fetchrow_hashref};
    seems to do what i want, though i'm worried what would happen if there's more than 1 result row...