CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
db hashref code

 



YankeeFan
New User

May 27, 2009, 11:03 AM

Post #1 of 4 (742 views)
db hashref code Can't Post

Greetings all,
Given this code, I cannot get a print line. I know that rowData->$myCol is wrong, but I do not know what it should be. I've tried different things without success.

This is a short segment of what I'm doing. I will not know what is in $myCol, but I do know it will match a column name in my "some_table". I need to do it this way.

@cols = ('fname','lname','addr','phone');
$foxSth = $foxDbh->prepare("SELECT * FROM some_table");
$foxSth->execute;
while ($rowData = $foxSth->fetchrow_hashref) {
foreach my $myCol (@cols) {
print $rowData->$myCol; # <-- doesn't work.
}
}

I've tried:
print $rowData->{$myCol};
print $rowData->[$myCol];
print $rowData[$myCol];

Any ideas or help?
Thanks in advance.


alex_v2
User

May 27, 2009, 12:14 PM

Post #2 of 4 (739 views)
Re: [YankeeFan] db hashref code [In reply to] Can't Post

You should do the following.


Code
use Data::Dumper; 

warn Dumper($rowData);


This will at least tell you what is in the hash and if it is formatted the way you are expecting.

Just as a side note from programing with DBs all my life you should never do a select *. You shoudl always control the return of DB by selecting exactly what you want. Also you might want to look at bind_col() method for DBI.

-Alexander


YankeeFan
New User

May 27, 2009, 1:35 PM

Post #3 of 4 (735 views)
Re: [alex_v2] db hashref code [In reply to] Can't Post

Thanks for the tip. I tried using Dumper($rowData) but that didn't return anything - warn Dumper($rowData) did.

I'm able to get printouts of what's in $rowData and what is in $myCol.
If $myCol = 'fname' and I see a $rowData->{'fname'} when printing debug statements, I know they are both present. I just need to know how to access the fields using $rowData and $myCol. Is there a way to use something like $rowData->$myCol? The syntax is not working.


alex_v2
User

May 27, 2009, 2:00 PM

Post #4 of 4 (732 views)
Re: [YankeeFan] db hashref code [In reply to] Can't Post

Yeah I use this all the time but the following syntax works for me and you have stated it does not work for you.


Code
$rowData->{$field};


-Alexander

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives