0

I am using an ajax script to send parameters and get a response to ajax.php. This is working fine as I can send parameters and get a response. My problem is that when I echo the query in the ajax.php script, I check on my browsers network. I see that the query is not complete and thus returns in an unexpected result. But when I execute the query within phpMyAdmin then I get the correct result. Please help.

This is the actual query below

$lat = $_REQUEST['lat'];
$lon = $_REQUEST['lng'];

$db = new MysqliDb ('uveehae_road');
$results= $db->rawQuery('SELECT id, address, ( 6371 * acos( cos( radians('.$lat.') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('.$lon.') ) + sin( radians('.$lat.') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 100 ORDER BY distance LIMIT 0,1');

This is what I see when I print the query and check on browser network

SELECT id, address, ( 6371 * acos( cos( radians(-26.0749518) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(28.1167666) ) + sin( radians(-26.0749518) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance 

As you can see the query is not complete but I don't know why.

Dharman
  • 30,962
  • 25
  • 85
  • 135
user7498826
  • 173
  • 3
  • 9

2 Answers2

2

As far as I remember, the author of that MysqliDb you are using has absolutely no knowledge in SQL, and made their class fail in situations like this. Let alone it's fatally prone to SQL injection.

You better quit using it and start with PDO prepared statements.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

So the most basic worked for me..

$lat = $_REQUEST['lat'];
    $lon = $_REQUEST['lng'];

    $servername = "localhost";
    $username = "user";
    $password = "password";
    $dbname = "dbname";

    $conn = new mysqli($servername, $username, $password,$dbname);

    $sql = "SELECT id,address, ( 6371 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lon) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 100 ORDER BY distance LIMIT 0,1";

    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
           echo json_encode($row);
        }
    }
user7498826
  • 173
  • 3
  • 9