我编写了以下功能,可根据搜索查询搜索用户:
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;
}
}
}
我尝试的是让用户首先根据用户名($ people)查找用户,然后根据距离,性别,年龄,方向等对这些用户进行过滤。($ get_users )。目前它不断给出循环(它继续加载但没有出现结果)。如果仅通过注释$ get_users并将$ people更改为$ get_users来使用$ people SQL查询,则会加载结果。我已经检查过数据库中的拼写错误(MyISAM)。我的代码中有错误或错过了什么吗?
我要感谢所有事先以任何方式提供帮助的人。
答案 0 :(得分:0)
要解决我自己的问题,以便将来可以帮助某人...首先,我没有将变量放在secure()函数中。其次,我调整了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;
}
}
}