基于位置更新数据库和查询

时间:2015-08-19 12:41:55

标签: php android jquery mysql location

在过去的几天里,我一直在尝试修复Android中的代码,以便我可以将OnLocationChanged()发送到数据库的新纬度和经度,并且当它更新时我希望它根据位置进行查询。但是我在做android时遇到了麻烦,我想知道是否有办法“组合”我用于更新和查询数据库的两个php代码。两个代码分开工作,但我可以合并吗? 我需要使用新数据更新数据库,然后查询,反之亦然。

Update.php

$latitude1 = $_POST["Latitude"];
$longitude1 = $_POST["Longitude"];
$username = $_POST["Username"];

$updatequery = mysqli_prepare($con , "UPDATE users SET Latitude = ?, Longitude = ? WHERE Username = ?");
mysqli_stmt_bind_param($updatequery ,"dds",$latitude1,$longitude1,$username);
mysqli_stmt_execute($updatequery);
 mysqli_stmt_close($updatequery);
mysqli_close($con);

QueryByLocation.php

$origLat = $_POST["Latitude"];
$origLon = $_POST["Longitude"];
$dist = 30;
$id = $_POST["ID"];

if(mysqli_connect_errno($con)) {
die("Connection failed : " . mysqli_connect_error());
}

$sql = "SELECT ID, Name, Age, City, Gender, Latitude, Longitude, 3956 * 2 * 1.609344 * 1000 *
          ASIN(SQRT( POWER(SIN(($origLat - abs(Latitude))*pi()/180/2),2)
          +COS($origLat*pi()/180 )*COS(abs(Latitude)*pi()/180)
          *POWER(SIN(($origLon-Longitude)*pi()/180/2),2))) 
          as distance FROM users WHERE ID != '$id'
          AND Longitude BETWEEN ($origLon-$dist/abs(cos(radians($origLat))*69)) 
          AND ($origLon+$dist/abs(cos(radians($origLat))*69)) 
          AND Latitude BETWEEN ($origLat-($dist/69)) 
          AND ($origLat+($dist/69))
          HAVING distance < $dist ORDER BY distance limit 30;"; 



$result = mysqli_query($con, $sql);

$rows = array();

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $rows[] = $row;
}

mysqli_close($con);
echo json_encode($rows);

1 个答案:

答案 0 :(得分:0)

$origLat = $_POST["Latitude"];
$origLon = $_POST["Longitude"];
$dist = 30;
$id = $_POST["ID"];
$username = $_POST["Username"];
if(mysqli_connect_errno($con)) {
die("Connection failed : " . mysqli_connect_error());
}

$updatequery = mysqli_prepare($con , "UPDATE users SET Latitude = ?, Longitude = ? WHERE Username = ?");
mysqli_stmt_bind_param($updatequery ,"dds",$origLat,$origLon,$username);
mysqli_stmt_execute($updatequery);
 mysqli_stmt_close($updatequery);

$sql = "SELECT ID, Name, Age, City, Gender, Latitude, Longitude, 3956 * 2 * 1.609344 * 1000 *
          ASIN(SQRT( POWER(SIN(($origLat - abs(Latitude))*pi()/180/2),2)
          +COS($origLat*pi()/180 )*COS(abs(Latitude)*pi()/180)
          *POWER(SIN(($origLon-Longitude)*pi()/180/2),2))) 
          as distance FROM users WHERE ID != '$id'
          AND Longitude BETWEEN ($origLon-$dist/abs(cos(radians($origLat))*69)) 
          AND ($origLon+$dist/abs(cos(radians($origLat))*69)) 
          AND Latitude BETWEEN ($origLat-($dist/69)) 
          AND ($origLat+($dist/69))
          HAVING distance < $dist ORDER BY distance limit 30;"; 



$result = mysqli_query($con, $sql);

$rows = array();

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $rows[] = $row;
}

mysqli_close($con);
echo json_encode($rows);