SQL help
Posted: Wed Aug 03, 2005 4:45 am
I want to pick out all of my usernames from table 'users' where their age is < 18.
I have their age stored as their birthdate in 3 separate fields -> agemonth, ageday, age year
I have attempted the following queries to do this, but it seems to pick out all of the users, ignoring my WHERE $years < 18 clause
The $years is correct.. I print it and I get the correct age for each person. I just can't seem to get it to query for people unde 18
I have their age stored as their birthdate in 3 separate fields -> agemonth, ageday, age year
I have attempted the following queries to do this, but it seems to pick out all of the users, ignoring my WHERE $years < 18 clause
Code: Select all
$query = mysql_query("SELECT agemonth, ageday, ageyear FROM users WHERE agemonth != ''"); // initial query to turn birthdate into age in years
while($array = mysql_fetch_assoc($query))
{
$month = $array['agemonth']; // this should
$day = $array['ageday']; // turn their
$year = $array['ageyear']; // birthdate
$seconds = strtotime("$month $day, $year"); // into a
$time = time(); // variable
$diff = $time - $seconds; // named $years
$years = substr($diff/31556926,0,2); // for 2nd query
$query2 = mysql_query("SELECT username FROM users WHERE $years < '18'"); // this should pick out users under 18, correct?
while($array2 = mysql_fetch_assoc($query2))
{
echo $array2['username'];
echo "<BR>";
}
}