我遇到这个问题,php将在json echo中返回重复的行,它在数据库本身工作,只是php在某处弄乱它而且我不确定在哪里。
<?php
$con = mysql_connect("host","user","pass");
if (!$con)
{
die('could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$sql = "SELECT DISTINCT INFO, ( 6364 * acos( cos( radians(55.952) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-3.187) ) + sin( radians(55.952) ) * sin( radians( lat ) ) ) ) AS distance FROM table HAVING distance <0.5 ORDER BY distance LIMIT 0 , 20";
$result = mysql_query($sql,$con);
if (!$result)
{
echo "db error\n";
echo 'MySQL Error : ' . mysql_error();
}
while ($row = mysql_fetch_array($result))
{
$output[]=$row["INFO"]; //added ["INFO"] to protect my location and make the problem easier to see.
echo (json_encode($output));
}
?>
它返回:
["This is where I study"]["This is where I study","this is where I live"]["This is where I study","this is where I live","this is where I could swim"]
即。 = [0][0,1][0,1,2]
我需要它只返回[0,1,2]
进行java操作。
先谢谢你,我是php / mysql的新手,所以欢迎任何评论。
答案 0 :(得分:2)
马克是正确的,你需要做这样的事情:
while ($row = mysql_fetch_array($result))
{
$infos[]=$row["INFO"]; //added ["INFO"] to protect my location and make the problem easier to see.
}
foreach ($infos as $info) {
echo (json_encode($info));
}