2014-01-21 161 views
0

我遇到了這個問題,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的新手,所以任何評論它歡迎。

+2

你呼應輸出你的'while'環......回聲__after__的每次迭代中返回的每一行建立起來完成'while'循環 –

回答

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)); 
} 
+0

給Mark和Mike謝謝你的回覆:) – DrBlackstar