当我添加LIMIT时,我的SQL请求什么也不返回

时间:2019-06-12 14:10:33

标签: php mysql database

我不明白为什么当我在其中添加LIMIT时,我的SQL请求什么都不返回。如果我不满意,该请求就可以正常工作...

我已经尝试直接在phpMyAdmin中执行请求,并且确实返回了结果,为什么它不能通过我的代码工作?这是我的代码:

    public function searchMembers($research, $start_from_user, $number_of_users, $connected_user_id) {
        $db= $this->MySQLConnect();
        $query = 'SELECT users_parameters.id, 
                        gender,
                        county,
                        username,
                        GROUP_CONCAT(interest_name SEPARATOR ", ") AS interests, 
                        birthdate, 
                        profile_picture
                FROM project_5_users_parameters AS users_parameters
                LEFT JOIN project_5_users_profiles AS users_profiles ON users_parameters.id = users_profiles.user_id
                LEFT JOIN project_5_users_interests AS users_interests ON users_parameters.id = users_interests.user_id
                LEFT JOIN project_5_interests AS interests_names ON users_interests.interest_id = interests_names.id
                WHERE (users_parameters.id != :connected_user_id AND (birthdate BETWEEN :start_birthdate AND :end_birthdate)';

        $execute =  ['connected_user_id' => $connected_user_id, 'start_birthdate' => $research->maxBirthday(),'end_birthdate' =>  $research->minBirthday()];

        if(!is_null($research->gender())) {
            $query .= ' AND gender = :gender';
            $execute['gender'] = $research->gender();

        }

        if(!is_null($research->county())) {
            $query .= ' AND county = :county';
            $execute['county'] = $research->county();
        }

        if(!empty($research->interests())) {
            foreach ($research->interests() as $key => $value) {
                if ($key === 0) {
                    $query .= ' AND (interest_id = :interest' . $key;
                }
                else  {
                    $query .= ' OR interest_id = :interest' . $key;
                }

                $execute['interest' . $key] = $value;
            }
            $query .= ')';
        }

        $query .= ') GROUP BY users_parameters.id LIMIT :start_from_user, :number_of_users';
        $execute['start_from_user'] = $start_from_user;
        $execute['number_of_users'] = $number_of_users;

        $req = $db->prepare($query);
        $req->execute($execute);


        $req->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'App\Entity\User');
        $result = $req->fetchAll();

        return $result;
    }

这是我传递给函数的参数:

array (size=6)
  'connected_user_id' => string '29' (length=2)
  'start_birthdate' => string '1920-06-12' (length=10)
  'end_birthdate' => string '2001-06-12' (length=10)
  'gender' => string 'Homme' (length=5)
  'start_from_user' => int 0
  'number_of_users' => int 4

等待您的帮助! :)

0 个答案:

没有答案