mok*_*211 1 php json web-services
我有用PHP编写的webservice,它从本地数据库读取并以JSON格式输出结果.
但是,我无法将其输出到JSONArray中.
这是php脚本
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
这是webservice的输出
{"name":"develop","country":"mru"}{"name":"fufu","country":"tutu"} {"name":"chikaka","country":"aceVentura"}
Run Code Online (Sandbox Code Playgroud)
但我被告知这不在JSONArray中.
我在这里错过了什么?
谢谢
在您的示例中,您回显了多个JSON字符串,因为您的输出代码在while循环中.JSON字符串应该只有一个输出.下面的代码将为您提供JSON格式的二维数组.
$info = array();
while ($row = mysql_fetch_assoc($result))
{
$arr = array();
$arr["name"] = $row["name"];
$arr["country"] = $row["country"];
$info[] = $arr;
}
echo json_encode($info);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2024 次 |
| 最近记录: |