[英]How to perform a php function within a mysql query
function SearchMembers()
{
// grab user input
//$my_gender = $this->security->xss_clean($this->input->post('my_gender'));
$this->security->xss_clean($this->input->post('looking_for_gender'));
$age_from = $this->security->xss_clean($this->input->post('age_from'));
$age_to = $this->security->xss_clean($this->input->post('age_to'));
$member_postcode = $this->security->xss_clean($this->input->post('postcode'));
$member_distance = $this->security->xss_clean($this->input->post('distance'));
// Prep the query
$this->db->where('Gender', $looking_for_gender);
$this->db->where('Age >=', $age_from);
$this->db->where('Age <=', $age_to);
$miles = $this->search_form_model->distCalc('postcode',$member_postcode);
$this->db->where($miles < $member_distance);
// Run the query
$query = $this->db->get('member');
// Let's check if there are any results
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
return $query->result();
}
// If the previous process did not validate
// then return false.
return false;
}
}
Hope someone can help. 希望有人能帮忙。 I'm performing a distance check between two postcodes in miles, and add all the ones that are less than distance to the result. 我正在以英里为单位执行两个邮政编码之间的距离检查,并将所有小于距离的邮政编码添加到结果中。
Previously all I have done is iterated through all postcodes that match gender and age then done a miles less than distance check and but the results in a json array, now a want to perform it within the mysql query if possible ideally using codeigniter framework. 以前,我所做的全部工作都是遍历所有匹配性别和年龄的邮政编码,然后比距离检查少一英里,但结果保存在json数组中,现在希望在MySQL查询中执行(如果可能的话,最好使用codeigniter框架)。 many thanks 非常感谢
First and foremost, use bound paramaters instead of XSS clean: 首先,使用绑定参数代替XSS clean:
See Does CodeIgniter automatically prevent SQL injection? 请参见CodeIgniter是否自动阻止SQL注入?
Assuming you have the longitude and latitude, you can add this to select the distance 假设您具有经度和纬度,则可以将其添加以选择距离
$this->db->select("
Gender,Age, --etc.
(
(
ACOS(
SIN($lat * PI() / 180)
* SIN(`lat` * PI() / 180)
+ COS($lat * PI() / 180)
* COS(`lat` * PI() / 180)
* COS(($lon - `lon`)
* PI() / 180))
* 180 / PI()
) * 60 * 1.1515
) AS distance
");
You will also need an order by
to sort, and a having
to only return values within the distance.. 您还需要一个order by
进行排序,并且having
只在距离之内返回值..
$this->db->order_by('distance');
$this->db->having('distance <= '.$member_distance.' OR postcode='.$member_postcode);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.