Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBI problem - populating array of hashes with fetchrow_hashref

Status
Not open for further replies.

jollekox

Programmer
Feb 3, 2004
23
NO
Hi!

I'm currently working on some kind of blogging technology for my website, and I'm having problems with a multidimensional data-structure used in a function to parse templates and output pages.

All data in the program is stored in an anonymous hash "bound" to a scalar ( [tt]my $self = {};[/tt] (yes, I'm trying to do object-oriented programming [smile] )).

So, I have an SQL query that pulls out an row of a database:
[tt]$self {sth} = $self {dbh}->prepare('select * from comments where id = ?');[/tt]

This statement is executed, and then I call fetchrow_hashref() as shown below:
[tt]$self {comments} [ $self {i} ] = $self {sth}->fetchrow_hashref();[/tt]

fetchrow_hashref() is executed in a while loop (together with the execution of the statement handle), and the fetched data stored in an array of hashes.

There must be some error in this line, because I am notified at runtime that it is "Not an ARRAY reference" when I try to do a [tt]print ${ $self {dataStructure} } [0] . "\n";[/tt] (The first (zeroth) slot of the array should be populated, since all while loops start at zero and increments by one for each iteration). The fetchrow_hashref() function works perfectly fine if I skip the array population part, like this: [tt]$self {post} = $self {sth}->fetchrow_hashref();[/tt].

So, is it a syntactical error? Or am I having problems dereferencing the variable? (It should be said that $self {dataStructure} is later passed into another function / subroutine as a reference, where the data is to be used).

I hope this explanation is understandable in spite of my bad English,
I will publish the relevant source code if anyone is interested.
Best regards,
Jo Andreas.
 
I'm not at all clear about your coding style. For OO stuff, it's traditional to have $self as a hashref but you're dereferencing it as a hash. I can't see where "dataStructure" is initialised - is it called "comments" earlier?

Try something more like
Code:
my $self = {};
$self->{sth} = $self {dbh}->prepare(
     'select * from comments where id = ?');
my $i = 0;
while( my $data = $self->{sth}->fetchrow_hashref() ) {
   $self->{comments}[$i++] = $data;
}
Your print statement,
Code:
print ${ $self {dataStructure} } [0] . "\n";
becomes something like this
Code:
print $self->{comments}[0], "\n";
although, as it's a hashref, it probably won't print what you want.

Code:
my $row = 0;
print join( "\n", map {
      my $field = $_;
      "$field -> $self->{comments}[$row]{$field}";
  } keys %{ $self->{comments}[$row] } );
might do a better job.

Yours,

fish

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top