Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with selectall_hashref

Posted on 2009-04-05
3
Medium Priority
?
1,678 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24072373
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 1700 total points
ID: 24072480
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
ID: 24072501
Sorry I made a mistake in the last sentence.

it should look like this.

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

715 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