0

我正在尝试设置一个查询,根据我表中的年龄显示我网站上所有 25 岁以下的用户ptb_stats.user_age。我设置了 -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>";
        }
    ?>
4

2 回答 2

1

我猜你正在寻找

SUBDATE(CURRENT_DATE(), INTERVAL 25 YEAR)
于 2013-02-15T18:48:53.507 回答
0

ptb_stats.userage数字还是时间戳?如果它是一个数字,那么where ptb_stats.userage < 25它将起作用。

于 2013-02-15T18:48:50.827 回答