我在从MySQL生成xml时出现问题,该问题似乎与邮件标题标题相关("内容类型:text / xml"); {{3} }指的是这个问题,但在我的情况下并没有解决问题。该帖子引用了2007 GMap示例,而不是This link
以下是完整的代码......
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a mySQL server
$connection=mysql_connect ("99.99.99", "abcdefg", "zzzzzz");
if (!$connection) {
die("Not connected : " . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db("xxxxxxx", $connection);
if (!$db_selected) {
die ("Can\'t use db : " . mysql_error());
}
// Search the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers ",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
// - !!!! This is the problem line !!!!!
//header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
$i = 1;
while ($row = mysql_fetch_assoc($result)){
echo $i.$row['name']."<br>"; // names correctly displayed
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", $row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("distance", $row['distance']);
$i = $i + 1;
}
echo $dom->saveXML();
echo "done ".$i; // Script ends ok $i = 16
一切看起来都像echo语句所示 - 但是,由于没有消息标题行,所以没有xml回显到屏幕。
我做错了什么?
答案 0 :(得分:-1)
答案似乎是不要忽略对弃用代码的警告 - 即使用mysqli_而不是mysql_!这就是我怀疑xml被认为格式错误的原因。
以下是不会出错的代码..
$con=mysqli_connect("servername","user","pwd","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Search the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers", // HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysqli_real_escape_string($con, $center_lat),
mysqli_real_escape_string($con, $center_lng),
mysqli_real_escape_string($con, $center_lat),
mysqli_real_escape_string($con, $radius));
//$result = mysql_query($query);
$result=mysqli_query($con,$query);
if (!$result) {
die("Invalid query: " . mysqli_error($con));
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", $row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();