如何从用PHP编写的Web服务输出JSONArray

I have webservice written in PHP that reads from the local database and output the result in JSON.

However, I am unable to output it into a JSONArray.

Here is the php script

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 

$response=array();

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("test",$dbhandle) 
or die("Could not select test");

//execute the SQL query and return records
$result = mysql_query("SELECT name, country FROM android");

$response["infos"] = array();   

while ($row = mysql_fetch_assoc($result)) {

    $info = array();
$info["name"]=$row["name"];
$info["country"]=$row["country"];

    print(json_encode($info));

}

//close the connection
mysql_close($dbhandle);
?>

This is the output from the webservice

{"name":"develop","country":"mru"}{"name":"fufu","country":"tutu"}  {"name":"chikaka","country":"aceVentura"}

But I have been told that this is not in JSONArray.

What am I missing here?

Thank you

In your example you're echo'ing out multiple JSON strings because your output code is within a while loop. There should only be one output for the JSON string. The code below will give you a two dimensional array in JSON format.

$info = array();

while ($row = mysql_fetch_assoc($result)) 
{
     $arr = array();
     $arr["name"] = $row["name"];
     $arr["country"] = $row["country"];
     $info[] = $arr;
}

echo json_encode($info);