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 Link to comment https://forums.phpfreaks.com/topic/251000-select-query-help-for-if-clauses/ 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? Link to comment https://forums.phpfreaks.com/topic/251000-select-query-help-for-if-clauses/#findComment-1287616 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"; Link to comment https://forums.phpfreaks.com/topic/251000-select-query-help-for-if-clauses/#findComment-1287645 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? Link to comment https://forums.phpfreaks.com/topic/251000-select-query-help-for-if-clauses/#findComment-1287683 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] => Link to comment https://forums.phpfreaks.com/topic/251000-select-query-help-for-if-clauses/#findComment-1287717 Share on other sites More sharing options...
fenway Posted November 13, 2011 Share Posted November 13, 2011 That doesn't answer the question Link to comment https://forums.phpfreaks.com/topic/251000-select-query-help-for-if-clauses/#findComment-1287786 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.