Greetings all,
Here are a few suggestions for your original code from what I could tell was going on. It looks close just a few things I would change.
my $sql = "SELECT * FROM admin WHERE day = ? AND month = ? and year = +? AND NUM = ? AND status= ?"; $sql .= " ORDER BY name" if ($order == 2); $sth = $dbh->prepare($sql) || die $dbh->errstr; my $rv = $sth->execute($d, $m_num, $y, $num, '2') || die $sth->errstr; if($rv && ($rv ne '0E0')){ while(my $pointer = $sth->fetchrow_hashref()){ print "NAME = ".$pointer->{'name'}."<br />"; } }
First: Use placeholder '?' for your sql statements, they allow you to prepare a statement once and reuse it.
Second: As others have stated watch out for the scope of your variables. my $name is only visible (scoped) within the while loop so the other reference to $name is not the $name from within the while loop. I would suggest useing warnings and strict
#!/usr/bin/perl -w use strict;
to find variable conflicts like this.
Third: check the return values of your db executes for success

that is all for my suggestions. As for why your data is not being sorted the way its ordered from the db I would suggest either printing from within the while loop or create an array of hashes (though this might not be the best idea depending on how many results you are looking at potentially getting from this query). Either way although the hash itself will not be ordered at least its position relative to the others (sql order by) will be.
-injunjoel

In reply to Re: Perl AND SQL Help by injunjoel
in thread Perl AND SQL Help by Anonymous Monk

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.