我遇到了这个问题,PHP将在json echo中返回重复的行,它在数据库本身中工作,只是php将它搞乱了,我不确定哪里。MySQL PHP查询返回重复结果,甚至使用DISTINCT
<?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]
,我需要它为Java操作简单[0,1,2]
返回。
预先感谢您,我是一个PHP/MySQL的新手,所以任何评论它欢迎。
你呼应输出你的'while'环......回声__after__的每次迭代中返回的每一行建立起来完成'while'循环 –