in reply to Re: Foreach Problem! Help!!!
in thread Foreach Problem! Help!!!

HI,
$order change to change the ORDER BY value.

Replies are listed 'Best First'.
Re^3: Foreach Problem! Help!!!
by Roy Johnson (Monsignor) on May 25, 2004 at 18:25 UTC
    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.
        Your results do not come out in the order you expect because you are doing multiple queries in arbitrary order. You are doing one query for each value in %expal.

        It is not the same thing to do this:

        SELECT * FROM foo WHERE expal = 5 ORDER BY expal; SELECT * FROM foo WHERE expal = 3 ORDER BY expal; SELECT * FROM foo WHERE expal = 2 ORDER BY expal; SELECT * FROM foo WHERE expal = 4 ORDER BY expal; SELECT * FROM foo WHERE expal = 1 ORDER BY expal;
        as it is to do
        SELECT * FROM foo WHERE expal IN (5,3,2,4,1) ORDER BY expal;
        Your code is effectively doing the former. You seem to want it to do the latter.

        The PerlMonk tr/// Advocate
Re: Re: Re: Foreach Problem! Help!!!
by Anonymous Monk on May 25, 2004 at 17:43 UTC
    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'