1

Having a weird issue with JSON_ENCODE. I am using php to retrieve data from a database and JSON to pass it to Javascript. It was working perfectly until I included another php file that I intend to use to do some processing on the data before it is returned. After including the file JSON started encoding both the data returned and the contents of the file that I included.

php code:

    <?php
    include("GeoLocation.php");//STATEMENT CAUSING JSON TO ACT WEIRDLY
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "go_with_your_mood";

        //$lat=(isset($_GET['lat']))?$_GET['lat']:'';
        //$long=(isset($_GET['long']))?$_GET['long']:'';


    //$geo = new GeoLocation();


$mysqli = new mysqli('127.0.0.1', $dbuser, $dbpass, $dbname);   
//Select Database
//mysql_select_db($dbname) or die(mysql_error());

//build query
$query = "SELECT * FROM service where service_type = 'security' limit 5";

//Execute query
$qry_result = $mysqli->query($query);

//initial array to encapsulate each individual row
$jsonArray = array();

// Insert a new array for each row returned
while($row = $qry_result->fetch_assoc()){
    $rowArray = array($row["id"],$row["name"],$row["address"],$row["suburb"],$row["postcode"],$row["phone"],$row["latitude"],$row["longitude"],$row["description"],$row["service_type"]);
    array_push($jsonArray, $rowArray);
}
echo json_encode($jsonArray);
?>

data that is returned from this file:

enter image description here

enter image description here

enter image description here

Data that should be returned:

    336,TERANG PUBLIC HOSPITAL,13 AUSTIN AVENUE,TERANG,3264,,-38.23939895629883000000,142.90240478515625000000,,medical,
337,ALFRED PUBLIC HOSPITAL,55 COMMERCIAL ROAD,MELBOURNE,3004,,-37.84560012817383000000,144.98210144042970000000,,medical,
338,CAULFIELD PUBLIC HOSPITAL,260 KOOYONG ROAD,CAULFIELD,3162,,-37.88240051269531000000,145.01669311523438000000,,medical,339,NORTHERN PUBLIC HOSPITAL,185 COOPER STREET,EPPING,3076,,-37.65259933471680000000,145.01510620117188000000,,medical,340,MAFFRA PUBLIC HOSPITAL,42-48 KENT STREET,MAFFRA,3860,,-37.96120071411133000000,146.98339843750000000000,,medical

Does anyone know why JSON is returning the contents of the included file as well as my data?

1 Answer 1

1

Seems like short tags <? are disabled. And looks like your geolocation.php file starts with <? instead of <?php Either edit your geolocation.php file and replace <? with <?php in the start.

Or include it like following

include( 'GeoLocation.php' );

Meaning include it by putting space before and after or copy paste the include code at it is written above. Hopefully one of these solutions work.

Alternatively, if you can then enable short tags.

1
  • That was it. Thanks for the quick fix. :) Aug 23, 2016 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.