toadi has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Weird DBI behaviour
by Joost (Canon) on Aug 19, 2005 at 12:10 UTC | |
by toadi (Chaplain) on Aug 19, 2005 at 12:18 UTC | |
by Joost (Canon) on Aug 19, 2005 at 13:21 UTC | |
Re: Weird DBI behaviour
by davidrw (Prior) on Aug 19, 2005 at 12:16 UTC | |
by toadi (Chaplain) on Aug 19, 2005 at 12:21 UTC | |
Re: Weird DBI behaviour
by punkish (Priest) on Aug 19, 2005 at 12:43 UTC | |
by Joost (Canon) on Aug 19, 2005 at 13:23 UTC | |
by jZed (Prior) on Aug 19, 2005 at 15:45 UTC | |
by punkish (Priest) on Aug 19, 2005 at 16:38 UTC | |
by jZed (Prior) on Aug 19, 2005 at 16:47 UTC |