Solved

Help with selectall_hashref

Posted on 2009-04-05
3
1,610 Views
Last Modified: 2012-05-06
I have this code here, but I'm not shure how can I access the second value from my query in the for loop.

Any help it would be great.

Thank you.
my $ldoc = {}; my $dSQL = "SELECT docID FROM tbl_doc"; 

$sth  = $dbh->prepare($dSQL); 

$sth->execute; $ldoc = $dbh->selectall_hashref($dSQL, 'docID', 'docTitle');
 

foreach $x (keys %$ldoc){

   print "$x\n"; 

}

Open in new window

0
Comment
Question by:Trexgreen
  • 2
3 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
Your query only has one column in the result set.

If you add docTitle to the SELECT, then in your loop you probably just need to acces it via

$ldoc{docTitle}
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 425 total points
Comment Utility
A couple of other things: Since you are using prepare() you don't need to use both execute AND selectall_hashref. selectall_hashref does an execute. You can even have selectall_hashref do the prepare part as well, so all you need is a 1 liner, but if you are going to call the statemnet multiple times you should call prepare first, and then pass $sth to selectall_hashref

Also, I usually use all uppercase in the column names, because Perl hash keys are case sensitive and most databases that I work with return column headers in uppercase.

my $dSQL = "SELECT DOCID, DOCTITLE FROM tbl_doc";

my $sth = $dbh->prepare($dSQL);

my $ldoc = $dbh->selectall_hashref($sth, "DOCID");
 

for my $docID (keys %$ldoc) {

   print "$docID -> $ldoc->{$docID}{DOCTITLE}\n";

}

Open in new window

0
 

Author Comment

by:Trexgreen
Comment Utility
Sorry I made a mistake in the last sentence.

it should look like this.

$docID = $dbh->selectall_hashref($dSQL, ['docID', 'docLink']);
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now