npsari Posted June 16, 2007 Share Posted June 16, 2007 Hi users! I use this query to fetch results... $q = "SELECT * FROM profile WHERE Country='$Country' AND Gender='$Gender' AND Age='$Age' ORDER BY Date DESC"; But users sometime select Any country, or Any age, etc.. How can i cancel Country='$Country' or Gender='$Gender' etc.. if a user chose Any I can't use if function because the possibilities are endless Quote Link to comment https://forums.phpfreaks.com/topic/55842-a-problem-retreiving-data-from-the-databse/ Share on other sites More sharing options...
soycharliente Posted June 16, 2007 Share Posted June 16, 2007 EDIT: I said something stupid here. I took it down because it was completely wrong. Quote Link to comment https://forums.phpfreaks.com/topic/55842-a-problem-retreiving-data-from-the-databse/#findComment-275876 Share on other sites More sharing options...
npsari Posted June 16, 2007 Author Share Posted June 16, 2007 I mean, if a user chooses Country = Any country (From an HTML form) then, all countries shows up in the results Which means, the Country='$Country' will not apply Quote Link to comment https://forums.phpfreaks.com/topic/55842-a-problem-retreiving-data-from-the-databse/#findComment-275879 Share on other sites More sharing options...
soycharliente Posted June 16, 2007 Share Posted June 16, 2007 Oh ok. I misread your question the first time. Try an OR statement wrapped in ()s or something. $q = "SELECT * FROM profile WHERE (Country='$Country' OR Country='Any') AND Gender='$Gender' AND (Age='$Age' OR Age='Any') AND Country!='Any' AND Age!='Any' ORDER BY Date DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/55842-a-problem-retreiving-data-from-the-databse/#findComment-275882 Share on other sites More sharing options...
aniesh82 Posted June 16, 2007 Share Posted June 16, 2007 Hello, You need to check whether the user selects any country and creates SQL query according to that. For Example: if($_GET['country'] == "Any") $countrySql = ""; else $countrySql = " AND Country= '".$_GET['country']."' "; Create sql query for all of the drop downs and finally make your sql statement as: $q = "SELECT * FROM profile WHERE 1 $countrySql "; Regards Aniesh Joseph anieshjoseph@gmail.com Quote Link to comment https://forums.phpfreaks.com/topic/55842-a-problem-retreiving-data-from-the-databse/#findComment-275883 Share on other sites More sharing options...
npsari Posted June 16, 2007 Author Share Posted June 16, 2007 Thanks charlieholder for your reply However, your above code will search for a Country called Any I dont want that actually I want when a users selects Any... The database simply shows all the countries but if the user choses USA, then, database searches only for USA Quote Link to comment https://forums.phpfreaks.com/topic/55842-a-problem-retreiving-data-from-the-databse/#findComment-275885 Share on other sites More sharing options...
npsari Posted June 16, 2007 Author Share Posted June 16, 2007 So can i say... if($_GET['country'] == "Any") $countrySql = ""; else $countrySql = " AND Country= '".$_GET['country']."' "; if($_GET['age'] == "Any") $ageSql = ""; else $ageSql = " AND Age= '".$_GET['age']."' "; $q = "SELECT * FROM profile WHERE 1 $countrySql AND $ageSql "; is that ok? and What is the 1 by the way? Quote Link to comment https://forums.phpfreaks.com/topic/55842-a-problem-retreiving-data-from-the-databse/#findComment-275886 Share on other sites More sharing options...
aniesh82 Posted June 16, 2007 Share Posted June 16, 2007 Hello A slight changes on your query . Please remove the "AND". Because we already added AND in $countrySql and $ageSql. $q = "SELECT * FROM profile WHERE 1 $countrySql $ageSql "; Suppose that at the first time you calling the page, do not have any "country or age " in the URL . So if you omit the 1 , the query will looks like this: $q = "SELECT * FROM profile WHERE " ; /* Nothing after the WHERE */ I think you got the idea now. Quote Link to comment https://forums.phpfreaks.com/topic/55842-a-problem-retreiving-data-from-the-databse/#findComment-275887 Share on other sites More sharing options...
npsari Posted June 16, 2007 Author Share Posted June 16, 2007 Thanks for the support Yep, i got the idea now Quote Link to comment https://forums.phpfreaks.com/topic/55842-a-problem-retreiving-data-from-the-databse/#findComment-275891 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.