简体   繁体   中英

Getting latitude and longitude from server using PHP

I am trying to build an Android app which queries a server to get the latitude and longitude for the given destination. However there seems to be an error in my PHP code as it shows the following error when I input the address in the web browser.

Notice: Undefined variable: destination in C:\xampp\htdocs\serverfiles\btc.php on line 6
{"result":[{"latitude":null,"longitude":null}]}

This is my btc.php file:

<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id  = $_GET['destination'];
$con = mysqli_connect("127.0.0.1", "root", "", "bustrack");

$sql = "SELECT * FROM updates WHERE destination='".$destination."'";
$r = mysqli_query($con,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"latitude"=>$res['latitude'],
"longitude"=>$res['longitude'],
)
);
echo json_encode(array("result"=>$result));
}

$sql = "SELECT * FROM updates WHERE destination='".$destination."'"; The variable $destination does not exist. You need to declare it before using it. I believe the variable $id is what you want, looking to your code.

This issue is that you never assign $destination a variable:

$id  = $_GET['destination'];
$con = mysqli_connect("127.0.0.1", "root", "", "bustrack");

$sql = "SELECT * FROM updates WHERE destination='".$destination."'";

You should do this:

$id  = $_GET['destination'];
$con = mysqli_connect("127.0.0.1", "root", "", "bustrack");

$sql = "SELECT * FROM updates WHERE destination='".$id."'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM