Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with selectall_hashref

Posted on 2009-04-05
3
Medium Priority
?
1,695 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

916 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