in reply to Re^2: passing variables to a sub
in thread passing variables to a sub

Whenever I see this in the httpd error log:
You have an error in your SQL syntax; check the manual ... for the right syntax to use near '...'
I know that the actual mistake usually precedes the part that is being quoted in the error message. So the next thing I do is to add something like this to the program, just before the $dbh->prepare( $sql ) call:
warn "$0: preparing sql: $sql\n";
Then I watch the error log to see what the whole sql statement text looks like, and this makes it easier to spot where I went wrong in putting it together.

In your case (based on other code you've posted earlier in this thread), I'm guessing that you have your "ORDER BY ..." and "WHERE ..." clauses in the wrong order ("WHERE ..." is supposed to come before "ORDER BY ...").