in reply to Foreach Problem! Help!!!

You're re-preparing and re-executing your query on each pass through the loop. Is that really what you want to do? Does the value of $order change in the loop?

You should be using placeholders for your variables. Something like:

$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"; } }
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.

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:

$sql = "SELECT * FROM tbl_admin WHERE day = '$d' AND month = '$m_num' +and year = '$y' AND expal IN (".join(',',keys %expal).") AND status= +'2'";
and no outer loop?

The PerlMonk tr/// Advocate

Replies are listed 'Best First'.
Re: Re: Foreach Problem! Help!!!
by Anonymous Monk on May 25, 2004 at 18:58 UTC
    The $order value is there only to say what SQL Query to run when user clicks on a button, the program prints a table with values and it has a button to select everything by alphabetic order that's when I am passing the value to $order.
    My question is to find out why the code doesn't work because of the first foreach loop on this line
    foreach my $key_expal (%expal) {...

    Where I need to extract the value of $key_expal to run the SQL query on the database.
Re: Re: Foreach Problem! Help!!!
by Anonymous Monk on May 25, 2004 at 17:32 UTC
    HI,
    $order change to change the ORDER BY value.
      But $order does not change within the loop, so there's no need to re-examine it and re-construct the query on each pass through.

      Secondly, when $order == 3, you have ORDER BY expal, but your query will only return one value for expal, so that doesn't do anything.


      The PerlMonk tr/// Advocate
        The $order value is there only to say what SQL Query to run when user clicks on a button, the program prints a table with values and it has a button to select everything by alphabetic order that's when I am passing the value to $order.
        My question is to find out why the code doesn't work because of the first foreach loop on this line
        foreach my $key_expal (%expal) {...

        Where I need to extract the value of $key_expal to run the SQL query on the database.
      It seems that the problem is when the values from the hash is evaluated on the foreach loop and to the SQL code by this
      AND EXPAL = '$key_expal'