Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Update: The second I posted this, I noticed that the while statement is making the fetchrow go out of scope (d'uh!). So the updated question is what is the most efficient/elegant way to achieve the structure that I need without it going out of scope?

Hello,

I'm receiving the "DBD::mysql::st fetchrow_hashref failed: fetch() without execute() " error when invoking fetchrow_hashref within loops. First the code:

my $sth_spaces = $dbh->prepare( "SELECT Spaces.*, SpaceTypes.SpaceTypeID,SpaceTypes.SpaceTypeName, COUNT(Spaces.SpaceID) from Spaces LEFT JOIN SpaceTypes ON SpaceTypes.SpaceTypeID = Spaces.Sp +aceTypeID WHERE Spaces.RegioID = $regioID GROUP BY SpaceID ORDER BY SpaceName "); $sth_spaces->execute(); my @regioloop = (); foreach my $regid ( sort { $a cmp $b } keys %$regia ){ my @insulaloop = (); foreach my $insid ( sort { $a cmp $b } keys %$insulae ){ my @spaceloop = (); while ( my $space = $sth_spaces->fetchrow_hashref() ){ push @spaceloop, { 'SpaceID' => $space->{'SpaceID'}, 'Doorway' => $space->{'Doorway'}, 'SpaceName' => $space->{'SpaceName'}, 'SpaceTypeID' => $space->{'SpaceTypeID'}, 'SpaceTypeName' => $space->{'SpaceTypeName'} }; } push @insulaloop, { 'InsulaID' => $insid, 'InsulaName' => $insulae->{ $insid }, 'SPACELOOP' => \@spaceloop }; } push @regioloop, { 'RegioID' => $regid, 'RegioName' => $regia->{ $regid }, 'INSULALOOP' => \@insulaloop }; push @{$fields->{'REGIOLOOP'}}, @regioloop; }
$fields is going to get tossed to HTML::Template for some happy TMPL_LOOP nesting, which is why I need this data structure.

If I pull the while statement outside of the foreach loops, it works fine. What is it about being inside the loop that makes this error come up, and how can I fix it, oh wise ones?

Thanks!
Jasmine


In reply to fetchrow_hashref within a loop yields "fetch() without execute()" by Jazz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-25 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found