mavera2 Posted November 12, 2011 Share Posted November 12, 2011 I'm trying to write a query for my MySQL database. I can successfully query the database but something i couldn't did. $firstcriteria='name'; $secondcriteria='age'; $ageMax='30'; $query = "SELECT * FROM mytable WHERE '$firstcriteria' IS NOT NULL AND IF ( '$firstcriteria' = 'mark', 1, ( IF ('$secondcriteria' < '$ageMax', 1, 0) ) ) AND ORDER BY '$secondcriteria' ASC; Where name is not null part: OK and if part: NOT OK I think there is an error of syntax etc. What would be the syntax when we use IF clause in sql queries for MySQL? Thank you Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 12, 2011 Share Posted November 12, 2011 There shouldn't be an AND before the ORDER BY clause, that much is certain. What error is MySQL returning when you execute the query? Quote Link to comment Share on other sites More sharing options...
mavera2 Posted November 12, 2011 Author Share Posted November 12, 2011 Thank you Pikachu for help. There shouldn't be an AND before the ORDER BY clause, that much is certain. What error is MySQL returning when you execute the query? No error appears because sql gives results, but false results. query1 gives right results, but the response of the query2 gives results which includes NULL and NOT NULL rows of firstcriteria $firstcriteria='name'; $query1 = "SELECT * FROM mytable WHERE name IS NOT NULL"; $query2 = "SELECT * FROM mytable WHERE '$firstcriteria' IS NOT NULL"; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 12, 2011 Share Posted November 12, 2011 Are they actually NULL, or are they just empty strings? Quote Link to comment Share on other sites More sharing options...
mavera2 Posted November 13, 2011 Author Share Posted November 13, 2011 Are they actually NULL, or are they just empty strings? Hmm, the results of query2 are empty strings like [name] => Quote Link to comment Share on other sites More sharing options...
fenway Posted November 13, 2011 Share Posted November 13, 2011 That doesn't answer the question Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.