1

I am in trouble to find a solution of my problem. I wrote an app which ask in a form an address and send a request to a mysql database through PHP. This is the extract of the APP code:

HttpPost httppost = new HttpPost("http://www.mywebsite.com/subfolder/selectall.php?PLAT=latitude_home&PLONG=longitude_home");

As you can see I am sending to the PHP page two parameters: Latitude and Longitude which are two DOUBLE variables.

The PHP code is the following:

$R_latitude = $_REQUEST['PLAT'];
$R_longitude = $_REQUEST['PLONG'];

$latitude = floatval($R_latitude);
$longitude = floatval($R_longitude);

$con = mysql_connect(server,database,password);
if (!$con) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);

$i=mysql_query("SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Lat`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Lat`*pi()/180)) * cos(((".$longitude."- `Lon`) * pi()/180))))*180/pi())*111.18957696) as distance FROM `Table` ORDER BY distance ASC LIMIT 10",$con);

It is supposed I get the results in order of distance from the point of interest, but what I get with this code is that the order is wrong, while if I use a fix value of latitude and longitude everything is fine:

// $latitude = floatval($R_latitude);
// $longitude = floatval($R_longitude);
$latitude = 45.4283585;
$longitude = 10.9669789;

What is wrong here ? IS someone able to help ? Thanks a lot and happy new year !

RlDDlCK
  • 606
  • 5
  • 10
Claudio
  • 25
  • 2
  • Use MySQLi not MySQL. – Script47 Jan 01 '16 at 12:04
  • 1
    Please dont use the `mysql_` database extensions, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Jan 01 '16 at 12:05
  • Have you tried inserting some fixed values into the URL in the first code snippet? – Martin Zabel Jan 01 '16 at 12:05
  • 5
    `mysql_select_db(dvname", $con);` MISSING QUOTE change to `mysql_select_db("dvname", $con);` – RiggsFolly Jan 01 '16 at 12:06
  • Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Jan 01 '16 at 12:44
  • If you say it works for you in case of fix values, then probably problem is with url which you exec, try to dump your url which you post in `HttpPost("url")` make sure `PLAT=45.4283585` and `PLONG=10.9669789` correctly set there – Armen Jan 01 '16 at 12:45
  • In fact there are no error so even if I add the error reporting display it will not show any error. The problem is that the results are unordered, while if I set the value giving http://www.mywebsite.com/subfolder/selectall.php?PLAT=45.4283585&PLONG=10.9669789 it works perfectly. What is strange is that if I show the PLAT and PLONG value received both are correct, so I really do not undesrtand why... – Claudio Jan 01 '16 at 13:58
  • So in summary if I call http://www.mywebsite.com/subfolder/selectall.php?PLAT=45.4283585&PLONG=10.9669789 it works perfectly with the correct order of distances, if I call http://www.mywebsite.com/subfolder/selectall.php?PLAT=latitude_home&PLONG=longitude_home with latitude_home=45.4283585 and longitude_home=10.9669789 WORKS, but the order of the answer is wrong (it is a completely disordered) – Claudio Jan 01 '16 at 14:00
  • I add one last thing... I discovered that into PHP page the two values received are considered NULL... if I run through browser http://mywebsite/subforder/selectall.php?PLAT=NULL&PLONG=NULL I got the same result unordered that I have with my code. So my problem is why I send two value but I cannot read them correctly ? Is it because are DOUBLE variables and not STRING ? – Claudio Jan 01 '16 at 14:18
  • What if you just instead use `$_GET['PLAT']` and `$_GET['PLONG']`, instead of `$_REQUEST` what will be the result then? – Niddro Jan 01 '16 at 14:36
  • I tried with GET insetad of REQUEST and I got the same result, but I made some progress in understanding the problem... the PHP is correct and the problem is only in Java, so the part related to the APP. I explain you why: if I use: HttpPost httppost = new HttpPost("http://mywebsite/internalfolder/selectall.php?PLAT=latitude_home&PLONG=longitude_home"); where latitude_home is a double variable and longitude_home is a double variable I got problems, while if I use HttpPost httppost = new HttpPost("http://mywebsite/internal folder/selectall.php?PLAT=45.4283585&PLONG=10.9669789"); all ok – Claudio Jan 01 '16 at 14:46
  • Can you either dump the query that is used when calling `mysql_query` or extract the query from the general log? That is the easiest way to check the executed query matches what you are trying to do. – Richard St-Cyr Jan 01 '16 at 14:55

1 Answers1

-1

Sorry guys... I made a really stupid mistake... instead of

`HttpPost httppost = new HttpPost("http://www.mywebsite.com/subfolder/selectall.php?PLAT=latitude_home&PLONG=longitude_home");`

I should have done:

`HttpPost httppost = new HttpPost("http://www.mywebsite.com/subfolder/selectall.php?PLAT="+latitude_home+"&PLONG="+longitude_home+"");`
Claudio
  • 25
  • 2