Writing this not to find a solution to a problem. But try to understand why something behaves that way.

I had to debug some code written by some else because sometimes it did't do what it needed todo.

We need to fetch paths to files from mysql but ordered by a certain field. The guy wrote following code:

my $select = $dbh->prepare("select path from application_table where +number = ? ORDER BY some_other_number"); $select->execute($number); my $array_ref = $select->fetchall_arrayref(); foreach my $row (@$array_ref) { foreach (@$row) { print "path: $_\n"; } }

for me this looked weird, but the coding style of other people always look weird. But the trouble whas that in some cases the paths printed weren't sorted anymore by some_other_number row in mysql.

I solved it like this:

my $select = $dbh->prepare("select path from application_table where +number = ? ORDER BY some_other_number"); $select->execute($number); my $array_ref = $select->fetchall_arrayref(); while(my($path) = $select->fetchrow_array) { print "PATH: $path\n"; }

Now it got sorted even in those rare cases it wasen't sorted. But I couldn't give a reasonable explanation why in some cases it wasen't sorted in the first example??? Can someone enlighten me???



--
My opinions may have changed,
but not the fact that I am right


In reply to Weird DBI behaviour by toadi

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.