简体   繁体   English

SQL选择用户并根据经度纬度过滤距离

[英]SQL Select the users and filter distance based on longitude latitude

I have written the following function which searches users based on a search query: 我编写了以下函数,该函数根据搜索查询来搜索用户:

        public function search_quick($query) {
        global $db, $system;
        $results = array();

        $person = $db->query(sprintf("SELECT * FROM users WHERE user_id = %s AND user_token = %s", secure($_COOKIE[$this->_cookie_user_id], 'int'), secure($_COOKIE[$this->_cookie_user_token]) )) or _error(SQL_ERROR_THROWEN);
            if($person->num_rows > 0) {
                $this->_data = $person->fetch_assoc();
            }

        $user_id = $this->_data['user_id'];
        $latitude = $this->_data['latitude'];
        $longitude = $this->_data['longitude']; 
        $user_gender = $this->_data['user_gender'];
        $user_sex = $this->_data['user_sex'];
        $sexual_preference = $this->_data['sexual_preference'];
        $sexual_orientation = $this->_data['sexual_orientation'];
        $age_range = explode(',',$this->_data['age_range']);
        $distance_range = explode(',',$this->_data['distance_range']);


        $people = $db->query(sprintf('SELECT * FROM users WHERE user_fullname LIKE %1$s LIMIT %2$s', secure($query, 'search'), secure($system['min_results'], 'int', false) )) or _error(SQL_ERROR_THROWEN);

    $get_users = $db->query(sprintf('
    SELECT *,
    3956 * 2 * ASIN(SQRT( POWER(SIN((%s - latitude) * pi()/180 / 2), 2) + COS(%s * pi()/180) * COS(latitude * pi()/180) *
    POWER(SIN((%s - longitude) * pi()/180 / 2), 2) )) as
    distance FROM %s
    HAVING distance >= %s AND distance <= %s AND user_id != %s AND user_gender = %s AND user_sex = %s AND age >= %s AND age <= %s', $latitude, $longitude, $longitude, $people, $distance_range[0], $distance_range[1], $user_id, $sexual_preference, $sexual_orientation, $age_range[0], $age_range[1] )) or _error(SQL_ERROR_THROWEN);

        if($get_users->num_rows > 0) {
            while($user = $get_users->fetch_assoc()) {
                $user['user_picture'] = $this->get_picture($user['user_picture'], $user['user_gender']);
                /* get the connection between the viewer & the target */
                $user['connection'] = $this->connection($user['user_id']);
                $user['sort'] = $user['user_fullname'];
                $user['type'] = 'user';
                $results[] = $user;
            }
        }
}

What I'm trying is to have it find the users first based on their user name ($people) and after that, have those users filtered based on distance, gender, age, orientation, etc. ($get_users). 我正在尝试让它首先根据用户名($ people)查找用户,然后再根据距离,性别,年龄,方向等($ get_users)筛选出这些用户。 Currently it keeps giving a loop (it keeps loading but no results are appearing). 目前,它一直在循环(它一直在加载,但没有结果出现)。 If only use the $people SQL query by commenting out $get_users and changing $people into $get_users, it does load results. 如果仅通过注释掉$ get_users并将$ people更改为$ get_users来使用$ people SQL查询,它将加载结果。 I have already checked for misspellings in the database (MyISAM). 我已经检查了数据库中的拼写错误(MyISAM)。 Is there a mistake in my code or have i missed something? 我的代码中有错误还是我错过了什么?

I would like to thank everybody who is helping out in any way beforehand. 我要感谢事先以任何方式提供帮助的每个人。

To awnser my own question so i can help someone in the future... First of all i didn't place the variables inside a secure() function. 为了回答我自己的问题,以便将来我可以帮助别人...首先,我没有将变量放在secure()函数中。 Second of all, I adjusted the SQL query so I only need 1. Underneath you will find the right working code. 其次,我调整了SQL查询,因此只需要1。在下面,您将找到正确的工作代码。

    public function search_quick($query) {
    global $db, $system;
    $results = array();

    $person = $db->query(sprintf("SELECT * FROM users WHERE user_id = %s AND user_token = %s", secure($_COOKIE[$this->_cookie_user_id], 'int'), secure($_COOKIE[$this->_cookie_user_token]) )) or _error(SQL_ERROR_THROWEN);
        if($person->num_rows > 0) {
            $this->_data = $person->fetch_assoc();
        }

    $user_id = $this->_data['user_id'];
    $latitude = $this->_data['latitude'];
    $longitude = $this->_data['longitude']; 
    $user_gender = $this->_data['user_gender'];
    $user_sex = $this->_data['user_sex'];
    $sexual_preference = $this->_data['sexual_preference'];
    $sexual_orientation = $this->_data['sexual_orientation'];
    $age_range = explode(',',$this->_data['age_range']);
    $distance_range = explode(',',$this->_data['distance_range']);

    $get_users = $db->query(sprintf('SELECT *,
3956 * 2 * ASIN(SQRT( POWER(SIN((%s - latitude) * pi()/180 / 2), 2) + COS(%s * pi()/180) * COS(latitude * pi()/180) *
POWER(SIN((%s - longitude) * pi()/180 / 2), 2) )) as
distance FROM users
HAVING distance >= %s AND distance <= %s AND user_fullname LIKE %s AND user_id != %s AND user_gender = %s AND user_sex = %s AND age >= %s AND age <= %s LIMIT %s', secure($latitude), secure($longitude), secure($longitude), secure($distance_range[0]), secure($distance_range[1]), secure($query, 'search'), secure($user_id), secure($sexual_preference), secure($sexual_orientation), secure($age_range[0]), secure($age_range[1]), secure($system['min_results'], 'int', false) )) or _error(SQL_ERROR_THROWEN);

    if($get_users->num_rows > 0) {
        while($user = $get_users->fetch_assoc()) {
            $user['user_picture'] = $this->get_picture($user['user_picture'], $user['user_gender']);
            /* get the connection between the viewer & the target */
            $user['connection'] = $this->connection($user['user_id']);
            $user['sort'] = $user['user_fullname'];
            $user['type'] = 'user';
            $results[] = $user;
        }
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM