<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2015-10-01 4:00 GMT-05:00 Dave Cross <span dir="ltr"><<a href="mailto:dave@dave.org.uk" target="_blank">dave@dave.org.uk</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
Quoting Richard Reina <<a href="mailto:gatorreina@gmail.com" target="_blank">gatorreina@gmail.com</a>>:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello All,<br>
<br>
I ussually iterate a hash that is the result of $rows_ref =<br>
$sth->fetchall_hashref('ID'); in my template with  <% FOREACH ID<br>
Matched_pats.keys.sort %>. However, In this case though the template only<br>
displays one row because each resulting row in the SQL query all have the<br>
same ID number. Is there any simple change I can make to get the results to<br>
fully iterate?<br>
</blockquote>
<br></span>
If you're putting data into a hash that's keyed on ID and every row has the same ID then you will only ever have one entry in your hash as each row that DBI puts into the hash will overwrite the previous one with the same ID. It's the same as writing code like this:<br>
<br>
  my $rows_ref = {};<br>
  $rows_ref->{'an_ID'} = 'foo';<br>
  $rows_ref->{'an_ID'} = 'bar';<br>
  $rows_ref->{'an_ID'} = 'baz';<br>
<br>
I hope it's obvious that this hash will only ever contain one key/value pair. Using fetchall_hashref('ID') with a constant value for 'ID' has exactly the same effect.<br>
<br>
As others have pointed out, the solution is to use a fetch method which returns an array rather than a hash. I'd add that in my experience (and I've been doing this a very long time) returning database data in an array (or an array of arrays) is almost always a better idea than returning a hash. Hashes certainly have their uses (that is, after all, why those methods exist), but arrays seem to match most people's model of a database resultset better than hashes do.<br>
<br>
In fact, now I come to think about it, the other problem that I helped you with recently was only as complex as it was because your data structure was a hash. In my solution, the first thing I did was to call values() on the hash which effectively turned it into an array.<br>
<br>
So I'd highly recommend using arrays rather than hashes in most instances when you're dealing with database resultsets.<br>
<br>
I'd also like to point out that not all of your problems are Dancer problems. The people on this list are, of course, really helpful. But the Perl community has mailing lists for all sorts of things. In particular, you might consider joining the mailing lists for:<br>
<br>
* DBI - <a href="http://lists.perl.org/list/dbi-users.html" rel="noreferrer" target="_blank">http://lists.perl.org/list/dbi-users.html</a><br>
* TT  - <a href="http://lists.perl.org/list/template-toolkit.html" rel="noreferrer" target="_blank">http://lists.perl.org/list/template-toolkit.html</a><br>
<br>
Hope this is helpful,<br>
<br>
Dave...<div class="HOEnZb"><div class="h5"><br></div></div></blockquote></div><br></div><div class="gmail_extra">Dave,<br><br></div><div class="gmail_extra">Thank you for the very thorough and instructive explanation. I will take your advice to heart.<br></div></div>