1
JSON_ENCODE有一個奇怪的問題。我使用PHP從數據庫中檢索數據,並使用JSON將其傳遞給Javascript。直到我包含另一個我打算用於在數據返回之前對數據進行處理的php文件,它才能完美工作。包含文件JSON開始編碼返回的數據和我包含的文件的內容。JSON_ENCODE編碼包含的php文件的內容
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);
?>
數據:
數據應該返回:
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會返回包含文件的內容以及我的數據?
就是這樣。感謝您的快速解決。 :) – user2787386