使用PHP和MySQL按年龄组对用户进行排序?

时间:2013-02-15 18:45:25

标签: php mysql

我正在尝试设置一个查询,根据表ptb_stats.user_age中的年龄显示我网站上25岁以下的所有用户。我设定了-25岁的间隔,但它没有像我期望的那样工作。我做错了什么?

我的功能:

function categories_agegroup_under25() {
            global $connection;
            $query = "SELECT *
                        FROM ptb_users, ptb_stats
                        WHERE ptb_users.account_type = \"User\" AND ptb_users.account_status = \"Active\" AND ptb_stats.user_id = ptb_users.id
                        AND ptb_stats.userage >= ADDDATE(CURRENT_DATE(), INTERVAL -25 YEAR)             

                        ORDER BY ptb_stats.user_age DESC
                        LIMIT 0,40";
            $categories_under25_set = mysql_query($query, $connection);
            confirm_query($categories_under25_set);
            return $categories_under25_set;
        }

我的php:

<?php
 $dob = $age['user_age'];

function age_from_dob($dob) {


       $date = date_diff(date_create($dob), date_create('now'))->y;  
       return $date;
}

?>
    <?php
        $categories_under25_set = categories_agegroup_under25();
        while ($age = mysql_fetch_array($categories_under25_set)) {
            $date = age_from_dob($age['user_age']);

            echo"
            <div class=\"boxgrid caption\">
            <a href=\"profile.php?id={$age['id']}\"><img width=140px height= 180px src=\"data/photos/{$age['id']}/_default.jpg\"/></a>
            <div class=\"cover boxcaption\">
            <h58> {$age['display_name']}, ".$date."</h58>
            </div>
            </div>";
        }
    ?>

2 个答案:

答案 0 :(得分:1)

我猜你正在寻找

SUBDATE(CURRENT_DATE(), INTERVAL 25 YEAR)

答案 1 :(得分:0)

ptb_stats.userage是数字还是时间戳? 如果它是一个比where ptb_stats.userage < 25更有用的数字。