JSON_ENCODE编码包含的php文件的内容

时间:2016-08-23 02:38:20

标签: php json

JSON_ENCODE遇到了一个奇怪的问题。我使用PHP从数据库中检索数据和JSON将其传递给Javascript。它工作得很好,直到我包含另一个php文件,我打算在返回之前对数据进行一些处理。包含文件后,JSON开始编码返回的数据和我包含的文件的内容。

php代码:

    <?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);
?>

从此文件返回的数据:

enter image description here

enter image description here

enter image description here

应返回的数据:

    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

有谁知道为什么JSON会返回包含文件的内容以及我的数据?

1 个答案:

答案 0 :(得分:1)

似乎禁用了短标记<?。看起来你的geolocation.php文件以<?而不是<?php开头 编辑geolocation.php文件,并在开头用<?替换<?php

或者包括如下

include( 'GeoLocation.php' );

含义通过在前后放置空格或复制粘贴包含代码来包含它,如上所述。希望其中一个解决方案有效。

或者,如果您可以启用短标签。