duanming7961 2015-06-05 05:37
浏览 58

Android php连接错误? (+ MySQL的)

I'm trying to get location(latitude, longitude) from android application and insert into database using php then extract data within a 10m radius.

The Problem is, when I test the code using smartphone (local test is OK), data is not correctly inserted. Table 'usergps' has 3 columns (name, latitude, longitude) and after I test the code, ( , 0, 0) is inserted.

Can you check the php code below? If it is correct, I think I should check the java code.

<?php
if(isset($_POST['name']) && $_POST['name'] != '') {
    require_once("include/db_info.php");
    $s=mysql_connect($SERV,$USER,$PASS) or die("fail to connect to mysql");
    mysql_select_db($DBNM);


    $lat = $_POST['lat'];
    $lon = $_POST['lon'];
    $name = $_POST['name'];

    $result= mysql_query("SELECT * FROM usergps WHERE name = '".$name."'");

    $row = mysql_num_rows($result);

    if($row == 0) {
        mysql_query("INSERT INTO usergps (name,latitude,longitude)
                    VALUES
                    ('".$name."', '".$lat."', '".$lon."')");

    }
    else {
        mysql_query("UPDATE usergps SET latitude = '".$lat."' WHERE name = '".$name."'");
        mysql_query("UPDATE usergps SET longitude = '".$lon."' WHERE name = '".$name."'");
    }


    $query = mysql_query("SELECT
                *,
        ( 6371 * acos( cos( radians('".$lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude )
- radians('".$lon."') ) + sin( radians('".$lat."') ) * sin( radians( latitude ) ) ) ) AS distance
            FROM usergps
            HAVING distance <= 0.01
            ORDER BY distance ASC");

    if(!$query) die ('Unable to run query:'.mysql_error());

    $numpeople = mysql_num_rows($query);
    echo json_encode($numpeople);

} else {
    $response["error"] = TRUE;
    $response["error_msg"]= "Required parameter 'name' is missing!";
    echo json_encode($response);
}
?>
  • 写回答

1条回答 默认 最新

  • douang4294 2015-06-05 06:24
    关注

    Please try to echo the data you get from Android app: $lat, $lon to check whether it posts correct data or not.

    评论

报告相同问题?