2014-12-11 86 views
1

我有一個PHP腳本,應該將所有位置從MySQL輸出到名爲allUsers.kml的文件。 時雖然創建的文檔是空的,它看起來像這樣:PHP腳本沒有正確生成來自MySQL數據的KML文件 - 如何正確處理這個問題?

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://www.opengis.net/kml/2.2"> 
    <Document/> 
</kml> 

的KML應該包含一個用戶,位置和這樣的運動的列表:

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://www.opengis.net/kml/2.2"> 
<Document> 
<Placemark> 
    <name>Anthony</name> 
    <userid>1899136</userid> 
    <sports>Running</sports> 
    <Point> 
     <coordinates>-2.799667,54.04569</coordinates> 
    </Point> 
</Placemark> 
</Document> 
</kml> 

,但它不。

我該如何解決我的PHP正確輸出這個文件?

<?php 
    header("Access-Control-Allow-Origin: *"); 
    $con=mysql_connect(PARAMS); 

    mysql_select_db("db"); 
    $user = $_POST['user']; 
    $lat = $_POST['lat']; 
    $lon = $_POST['lon']; 

    $sql = "UPDATE userActivityLocation SET lat='$lat', lon='$lon' WHERE user='$user'"; 
    $result=mysql_query($sql); 
    echo $result; 
    if (!mysql_query($sql, $con)) { 
     die('Error: ' . mysql_error()); 
    } else { 
     echo "Added"; 
    } mysql_close($con); 

    $sql2="SELECT * FROM userActivityLocation"; 
    $result2=mysql_query($sql2); 

    $dom = new DOMDocument('1.0', 'UTF-8'); 
    $dom->formatOutput = true; 
    // Iterate through the rows, adding KML nodes for each 
    // Creates the root KML element and appends it to the root document. 
    $node = $dom->createElementNS('http://www.opengis.net/kml/2.2', 'kml'); 
    $parNode = $dom->appendChild($node); 

    // Creates a KML Document element and append it to the KML element. 
    $dnode = $dom->createElement('Document'); 
    $docNode = $parNode->appendChild($dnode); 


    while ($row = mysql_fetch_assoc($result2)){ 
     $dnode = $dom->createElement("Placemark"); 
     $newnode = $docNode->appendChild($dnode); 

     $newnode = $dom->createElement("user"); 
     $newnode->nodeValue = $row['user']; 
     $dnode->appendChild($newnode); 

     $newnode = $dom->createElement("sports"); 
     $newnode->nodeValue = $row['sports']; 
     $dnode->appendChild($newnode); 

     //Coordinates are a child node of the 'Point' node  
     $pointnode = $dom->createElement("Point"); 
     $dnode->appendChild($pointnode); 

     $coordsnode = $dom->createElement("coordinates"); 
     $coordsnode->nodeValue = $row['lon'].",".$row['lat']; 
     $pointnode->appendChild($coordsnode); 
    } 

    $filename = "allUsers.kml"; 
    $dom->save($filename); 
    $msg = "PHP Working - Location - KML Generated"; 
    echo $msg; 
    echo "#####"; 
    echo $filename; 
?> 

我真的很想體會到任何幫助。謝謝。

回答

1

看起來您正在關閉「添加」後的SQL連接,因此您的選擇查詢沒有結果。

} else { 
    echo "Added";  
    } mysql_close($con); 

    $sql2="SELECT * FROM userActivityLocation"; 
    $result2=mysql_query($sql2); 

mysql_close($con);移動到文件最底部,您應該很好。

+0

啊謝謝你這麼多 - 我會試試。 – anthonyhumphreys 2014-12-11 03:04:26

+1

完美無缺!非常感謝 – anthonyhumphreys 2014-12-11 03:29:14