Solved

Help with selectall_hashref

Posted on 2009-04-05
3
1,616 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 425 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

937 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