When posting code, use the [code], [html] or [php] tags as appropriate.
If you're generating an SQL query that isn't working, check the generated statement. If you don't have a PHP debugger (e.g. Eclipse+PDT+XDebug), add some scaffolding to the script to print the statement . You'll see...