我有一个PHP脚本,它应该将MySQL中的所有位置输出到名为allUsers.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;
?>
我真的非常感谢任何帮助。谢谢。
答案 0 :(得分:1)
在添加&#39;之后,您似乎正在关闭SQL连接。因此您的选择查询没有结果。
} else {
echo "Added";
} mysql_close($con);
$sql2="SELECT * FROM userActivityLocation";
$result2=mysql_query($sql2);
将mysql_close($con);
向下移动到文件的最底部,你应该很好。