I want to show Merchant details approximately 60 miles distance through the User current location,
Below My coding,
$criteria = new CDbCriteria;
$criteria->select = '*, ( 3959 * acos( cos( radians(' . $currentlatval . ') ) * cos( radians( lat ) ) '
. '* cos( radians( lng ) - radians(' . $currentlngval . ') ) '
. '+ sin( radians(' . $currentlatval . ') ) * sin( radians( lat ) ) ) ) AS distance ';
$criteria->having = ("distance < 60");
$criteria->group = 'uid';
$results2 = Merchant::model()->findAll($criteria);
Its Working Properly,In the above code it check only attribute lat and lng,But I want to also check attribute city_lat and city_lng in above code How to write to check both attribute?
Related
How to find nearest cities(user location[lat, long]) from latitude and longitude in PHP Laravel?
Table Structure :
locations: 'lon, lat'
user location: 'lon, lat'
$loc = explode(',', $request->get('location'));
$results = Agent::select(Agent::raw('SELECT id, ( 3959 * acos( cos( radians(' . $loc[0] . ') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $loc[1] . ') ) + sin( radians(' . $loc[0] .') ) * sin( radians(lat) ) ) ) AS distance FROM articles HAVING distance < ' . 200 . ' ORDER BY distance') )->get();
$agents = \App\Agent::
where('name','like','%'.$request->get('text').'%')
->orWhere('manager','like','%'.$request->get('text').'%')
->orWhere('address','like','%'.$request->get('text').'%')
->orWhere('phone','like','%'.$request->get('text').'%')->get();
foreach ($agents as $agent) {
$agent->location = explode(',', $agent->location);
}
return $agents;
I'm trying to return results by latitude and longitude in me BD.
I have this script. It works fine when I do the phpmyadmin
SELECT `loc` . * , (
(
(
ACOS( SIN( ( 4.6665578 * PI( ) /180 ) ) * SIN( (
`latitude_advertiser` * PI( ) /180 ) ) + COS( ( 4.6665578 * PI( ) /180 ) ) * COS( (
`latitude_advertiser` * PI( ) /180 )
) * COS( (
(
- 74.0524521 - `longitude_advertiser`
) * PI( ) /180 )
)
)
) *180 / PI( )
) *60 * 1.1515
) AS `distance`
FROM `user_profile_` `loc`
HAVING `distance` <1
LIMIT 0 , 30
However, when I try to do the similar procedure using php my query Returns How Empty.
$connect = mysqli_connect('localhost','root','');
$db = mysqli_select_db($connect, 's2_001') or die ('Erro #1010');
$distance = 10;
$latitude = 4.6665578;
$longitude = -74.0524521;
$sql = "SELECT `loc`.*, (((acos(sin(($latitude*pi()/180)) * sin((`latitude_advertiser`*pi()/180))+cos(($latitude*pi()/180)) * cos((`latitude_advertiser`*pi()/180)) * cos((($longitude - `longitude_advertiser`)*pi()/180))))*180/pi())*60*1.1515) AS `distance` FROM `user_profile_` `loc` HAVING `distance` < 1";
$query = mysqli_query($connect, $sql);
while($row = mysqli_fetch_array($query)){
echo $row['id'] . '-' . $row['distance'] . '<br>';
}
echo mysqli_num_rows($query); // no line here
echo $sql; // test query, working in phpmyadmin
I'm putting together an API for an iOS app and need to get locations nearby a geo coordinate.
I tried this query in mysqli and it returns no results. When I do it in a regular mysql_query it works perfectly. I also tested the query in phpmyadmin where it completes successfully as well.
SELECT id, name, address, city, state, longitude, latitude, ( $miles * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) ) AS distance
FROM locations HAVING distance < $distance ORDER BY distance LIMIT 0, 20
This is the query with the variables filled in:
SELECT id, name, address, city, state, longitude, latitude, ( 3959 * acos( cos( radians(40.735767) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-73.991806) ) + sin( radians(40.735767) ) * sin( radians( latitude ) ) ) ) AS distance
FROM locations HAVING distance < 25 ORDER BY distance LIMIT 0, 20
Is there something I'm missing why it will not work in mysqli?
This is the working mysql query code
mysql_connect('localhost', 'test', 'test') or die(mysql_error());
mysql_select_db('testdb') or die(mysql_error());
$locations = array();
$miles = 3959;
$distance = 25;
$latitude = "40.735767";
$longitude = "-73.991806";
$data = mysql_query("SELECT id, name, address, city, state, longitude, latitude, ( $miles * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) ) AS distance
FROM locations HAVING distance < $distance ORDER BY distance LIMIT 0, 20")
or die(mysql_error());
while ($row = mysql_fetch_assoc($data)) {
array_push($locations, $row);
}
I'm using MysqliDb Class https://github.com/ajillion/PHP-MySQLi-Database-Class so that should work as follows
$row = $db->rawQuery("SELECT id, name, address, city, state, longitude, latitude, ( ? * acos( cos( radians(?) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?) ) + sin( radians(?) ) * sin( radians( latitude ) ) ) ) AS distance
FROM locations HAVING distance < ? ORDER BY distance LIMIT 0, 20", array($miles, $latitude, $longitude, $latitude, $distance));
if (count($row) > 0){
// if found, return JSON response
echo json_encode($row[0]);
}
Even when I use the basic template I found for mysqli it fails.
$locations = array();
$miles = 3959;
$distance = 25;
$latitude = "40.735767";
$longitude = "-73.991806";
// Connect to database
$link = mysqli_connect('localhost','test','test','testdb');
// Check for Errors
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
// Prepare Query
$query = "SELECT id, name, address, city, state, longitude, latitude, ( $miles * acos( cos( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( latitude ) ) ) ) AS distance
FROM locations HAVING distance < $distance ORDER BY distance LIMIT 0, 20";
// Escape Query
$query = mysqli_real_escape_string($link,$query);
// Perform Query
if($result = mysqli_query($link,$query)){
// Cycle through results
while($row = mysqli_fetch_object($result)){
array_push($locations, $row);
}
// Free Result Set
mysqli_free_result($result);
}
// Close Connection
mysqli_close($link);
Is there something I'm missing why it will not work in mysqli?
No.
Both mysql and mysqli APIs run your queries exactly the same way.
Look for the typos. And other errors of the kind.
I am running the following query in a PHP script:
$radius_query = "SELECT zip, ( 3959 * acos( cos( radians(" . $latitude .") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( " . $longitude . ") ) + sin( radians(" . $latitude .") ) * sin( radians( lat ) ) ) ) AS distance
FROM ZIP HAVING distance < ". $radius;
$radius_result = $db_zipcode->query($radius_query);
if (!$radius_result){
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = $radius_result->fetch(PDO::FETCH_ASSOC);
While($row = $radius_result->fetch(PDO::FETCH_ASSOC))
{
$zip[] = $row['zip'];
}
foreach ($zip as $zip_display){
echo $zip_display . ", " . '';
}
The query itself works with one minor error - it does not return the first row of data. It should return a list of zip codes similar to this:
40324, 40339, 40340, 40347, 40356...
This is the result I get when I run the same query in phpMyAdmin.
However when I run the query above in a PHP script, the result set starts with 40339, and then includes all the remaining zip codes. Its not a mileage issue (i.e. the missing zip is well within the radius - as it shows up when I run the query in phpMyAdmin).
What am I doing wrong in the PHP query to miss that first row of data? Its not just a display problem because when I insert the query results in a DB, it is also missing the first row.
Thanks!
Problem is in the first fetch, they take your first row...
Run this without
$row = $radius_result->fetch(PDO::FETCH_ASSOC);
I had the same problem about a day ago.
There's no need to do an implicit loop in the PHP, try
$zip = $radius_result->fetchAll(PDO::FETCH_ASSOC);
I have a SELECT SQL statement that returns a Latitude, Longitude and Distance as result (using the Haversine formula). Now I need to store these values as variables within the PHP script as I need to do further processing on the results. Can anyone tell me how I can do this?
Here is my SQL Statement
UPDATED
$query = $db->prepare("SELECT `Latitude`, `Longitude`, (6378160 * acos( cos( radians(:latitude) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(:longitude) ) + sin( radians(:latitude) ) * sin( radians( Latitude ) ) ) ) AS distance FROM `mytable` HAVING distance < :radius ORDER BY distance;");
// Assign parameters
$query->bindParam(':latitude',$newLatitude);
$query->bindParam(':longitude',$newLongitude);
$query->bindParam(':radius',$radius);
//Execute query
$query->execute();
$q = $db->query($query);
$q->setFetchMode(PDO::FETCH_ASSOC);
// fetch
while($r = $q->fetch()){
$eLatitude = $r->Latitude;
$eLongitude = $r>Longitude;
$distance = $r->Distance;
}
I updated my php code above. Is this code's syntax correct? Or the way I'm doing it is all wrong?
PDO::FETCH_ASSOC: returns an array indexed by column name Manual
while($r = $q->fetch()){
echo $r['Latitude']. "\n"; //Or do what ever instead of echo
echo $r['Longitude']. "\n";
echo $r['Distance']. "\n";
}
ie XML
while($r = $q->fetch()){
$node = $dom->createElement("marker");
$newnode->setAttribute("lat", $r['Latitude']);
$newnode->setAttribute("lng", $r['Longitude']);
$newnode->setAttribute("distance", $r['Distance']);
}
ie JSON
while($r = $q->fetch()){
data[] = $r;
}
you can use
$queryresult = mysql_query("SELECT `Latitude`, `Longitude`, (6378160 * acos( cos( radians(:latitude) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(:longitude) ) + sin( radians(:latitude) ) * sin( radians( Latitude ) ) ) ) AS distance FROM `mytable` HAVING distance < :radius ORDER BY distance;");
if (!$queryresult) {
echo 'Error ' . mysql_error();
exit;
}
$resultrow = mysql_fetch_row($queryresult);
echo $resultrow[0]; // this will be latitude
echo $resultrow[1]; // this will be longitude