Php connection error in android? (+ MySQL)

I am trying to get location (latitude, longitude) from android app and insert into database using php and then fetch data within 10m radius.

The problem is that when I test the code with a smartphone (local test is fine) the data is not inserted correctly. The 'usergps' table has 3 columns (name, latitude, longitude), and after checking the code, (0, 0) is inserted.

Can you check the PHP code below? If this 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);
}
?>

      

0


source to share


1 answer


Try to iterate over data received from android app: $ lat, $ lon to check if it is not sending correct data or not.



0


source







All Articles