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