Is it possible to call a function in my SELECT statement in php?

综合编程 2017-12-31

I have a database which holds different locations, each of which has its own longitude and latitude variable. I want to be able to use a distance function I made that returns the distance between two longitudes and latitudes in my WHERE statement. So I am looking for the distance between two points and it pass the WHERE statement if it is less than the radius I am searching.

distance function:

function distance($lat1, $lon1, $lat2, $lon2) {
    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    return $miles;

inside my SELECT statement I want something like this:

SELECT * FROM Locations WHERE distance($latitude, $longitude, 'latitude', 'longitude')

however that is not working. I have tried these without any luck...

"SELECT * FROM Locations WHERE distance($latitude, $longitude, 'latitude', 'longitude') < $radius"


sprintf("SELECT * FROM Locations WHERE %f < $radius", distance($latitude, $longitude, 'latitude', 'longitude'))

Is this even possible to preform? If not what is a good way around this problem? Thanks!

EDIT: I tried to do

"SELECT * FROM Locations WHERE '" . distance('Latitude', 'Longitude', $latitude, $longitude) . "' < $radius"

but it didn't work however if I just echo it it outputs the result I am looking for. But it doesn't work right when I try to use it inside mysqli_query

I have use this same but one issue: I have used in query as follow:

$sql = "select ... AND '".distance2($lat,$long,'',``) ."'<=20 ";

but in function:

function distance2($lat1,$long1,$lat2,$long2){
            //Google api

            echo $fromAddress=$lat1.','.$long1;
            echo $toAddress=$lat2.','.$long2;
                // echo "$fromAddress&destinations=$toAddress&sensor=false";
            $URL = json_decode(file_get_contents("$fromAddress&destinations=$toAddress&sensor=false"));

            echo "
".$URL; exit; $status = $URL->rows[0]->elements[0]->status; if($status != 'ZERO_RESULTS'){ $res = round($URL->rows[0]->elements[0]->distance->value/1000); return $res; } }

it gets 3rd and 4th param as follow: 3rd-> 4th->

Hello, buddy!

责编内容by:Hello, buddy! (源链)。感谢您的支持!


Assign the value of a global variable in php from ... I have an html file in which i have defined a global variable as query("SELECT * FROM rings"); $ma...
Building a Chat System in AJAX & PHP Description Featured on: May 17, 2018 Build a responsive and dynamic chat sys...
PHP中使用cURL 闲话已经两个月没有写简书了,百度地图实习这段时间还是深有体会的,虽然说实习的开发效率感觉不如一个人在学校时候开发效率高,但经过一个多月的实习,体验了一流互联网公司的开发流程,接触到了很多新...
Generate PDF File from MySQL Database Using PHP This is another export feature php tutorials, I will create simple PHP script to fetch data from My...
游戏安全资讯精选 2018年第五期:一年收购金额超100亿,都有哪些棋牌游戏公司上了岸?PHP GD... 摘要: 一年收购金额超100亿,都有哪些棋牌游戏公司上了岸?PHP GD库拒绝服务漏洞安全建议,软件集成平台Jenkins证书及日志泄露 ...