jattsurma Posted April 18, 2011 Share Posted April 18, 2011 Hi, I'm trying to figure out how to select all the males from database that are within the ages of 20 - 30. I can't get the query working. If someone can point me to right direction, it will be greatly appreciated. Here is my query: $query = "SELECT * FROM profiles WHERE sex={$_GET['sex']}, age >= 20, age <= 30 ORDER BY pId DESC"; this is what I get when I echo the $query: SELECT * FROM profiles WHERE sex=Male, age >= 20, age <= 30 ORDER BY pId DESC Thank in advance for your help. Jatt Surma Quote Link to comment https://forums.phpfreaks.com/topic/234035-mysql-query-getting-records-using-less-and-greater-than/ Share on other sites More sharing options...
raindropz12 Posted April 18, 2011 Share Posted April 18, 2011 Try this $query = "SELECT * FROM profiles WHERE sex={$_GET['sex']}, age>= 20 AND age<=30 ORDER BY pId DESC"; or $query = "SELECT * FROM profiles WHERE sex={$_GET['sex']}, age BETWEEN 20 AND 30 ORDER BY pId DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/234035-mysql-query-getting-records-using-less-and-greater-than/#findComment-1202889 Share on other sites More sharing options...
ignace Posted April 18, 2011 Share Posted April 18, 2011 SELECT * FROM profiles WHERE sex={$_GET['sex']} AND age>= 20 AND age<=30 ORDER BY pId DESC It's AND not , Quote Link to comment https://forums.phpfreaks.com/topic/234035-mysql-query-getting-records-using-less-and-greater-than/#findComment-1202913 Share on other sites More sharing options...
raindropz12 Posted April 18, 2011 Share Posted April 18, 2011 sorry I forgot to change the first comma into AND Quote Link to comment https://forums.phpfreaks.com/topic/234035-mysql-query-getting-records-using-less-and-greater-than/#findComment-1202939 Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2011 Share Posted April 18, 2011 Sorry to throw a monkey-wrench into your data storage, but age is not a constant value (unless you are dead.) Age changes once a year (for most people.) You need to store the date of birth and calculate the age. Quote Link to comment https://forums.phpfreaks.com/topic/234035-mysql-query-getting-records-using-less-and-greater-than/#findComment-1202940 Share on other sites More sharing options...
jattsurma Posted April 19, 2011 Author Share Posted April 19, 2011 Thanks for your help guys. I tried it out and it worked flawlessly. I appreciate it. Jatt Surma Quote Link to comment https://forums.phpfreaks.com/topic/234035-mysql-query-getting-records-using-less-and-greater-than/#findComment-1203366 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.