You should be using placeholders for your variables. Something like:
Because %expal is a hash, your queries will be run in arbitrary order, but the results of each query should be returned in the order your query specified.$sql =<<"SELECT * FROM tbl_admin WHERE day = '$d' AND month = '$m_num' + AND year = '$y' AND expal = '?' AND status= '2'"; if($order == 2 ) { $sql .= " ORDER BY c_name"; } elsif ($order == 3 ) { $sql .= " ORDER BY expal"; } $sth = $dbh->prepare($sql); foreach my $key_expal (keys %expal) { $sth->execute($key_expal) || die $sth->errstr; while ($pointer = $sth->fetchrow_hashref) { my $expalnum = $pointer->{'expal'}; my $comp = $pointer->{'c_name'}; my $cltime = $pointer->{'time'}; my $cltime2 = $pointer->{'time2'}; my $last_name = $pointer->{'last_name'}; my $clnum = $pointer->{'c_number'}; my $status = $pointer->{'status'}; print "<br>L 265 - <b>$comp</b> $expalnum"; } }
Ordering by expal is redundant, though, when you're specifying that only one expal is acceptable. You might need to rethink your query. Do you really want to use an IN:
and no outer loop?$sql = "SELECT * FROM tbl_admin WHERE day = '$d' AND month = '$m_num' +and year = '$y' AND expal IN (".join(',',keys %expal).") AND status= +'2'";
In reply to Re: Foreach Problem! Help!!!
by Roy Johnson
in thread Foreach Problem! Help!!!
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |