PHP 脚本未正确从 MySQL 数据生成 KML 文件 - 如何正确处理此问题?
Posted
技术标签:
【中文标题】PHP 脚本未正确从 MySQL 数据生成 KML 文件 - 如何正确处理此问题?【英文标题】:PHP Script not generating KML file from MySQL data properly - how to correctly approach this? 【发布时间】:2014-12-11 00:41:09 【问题描述】:我有一个 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;
?>
我真的非常感谢任何帮助。谢谢。
【问题讨论】:
【参考方案1】:看起来您在“添加”之后关闭了 SQL 连接,因此您的选择查询没有得到任何结果。
else
echo "Added";
mysql_close($con);
$sql2="SELECT * FROM userActivityLocation";
$result2=mysql_query($sql2);
将mysql_close($con);
移到文件的最底部,这样就可以了。
【讨论】:
以上是关于PHP 脚本未正确从 MySQL 数据生成 KML 文件 - 如何正确处理此问题?的主要内容,如果未能解决你的问题,请参考以下文章