googlemapapi获取XML声明只允许在文档错误开始时使用


google map api getting XML declaration allowed only at the start of the document error

我在这个链接上使用谷歌地图apihttps://developers.google.com/maps/articles/phpsqlajax_v3并尝试将其用于我的数据库。但是当我运行这个php代码时,我会出现错误。

这是我的数据库:

CREATE TABLE IF NOT EXISTS `Users` (
  `PID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `latitude` float(10,6) NOT NULL,
  `longitude` float(10,6) NOT NULL,
  `type` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `time` datetime NOT NULL,
  PRIMARY KEY (`PID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=36 ;

这是我修改的php代码:

<?php  
require("dbinfo.php"); 
// 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 ($host, $username, $password);
if (!$connection) {  die('Not connected : ' . mysql_error());} 
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can''t use db : ' . mysql_error());
} 
// Select all the rows in the users table
$query = "SELECT * FROM Users WHERE 1";
$result = mysql_query($query);
if (!$result) {  
  die('Invalid query: ' . mysql_error());
} 
header("Content-type: text/xml"); 
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){  
  // ADD TO XML DOCUMENT NODE  
  $node = $dom->createElement("marker");  
  $newnode = $parnode->appendChild($node);   
  $newnode->setAttribute("name",$row['name']);  
  $newnode->setAttribute("latitude", $row['latitude']);  
  $newnode->setAttribute("longitude", $row['longitude']);  
  $newnode->setAttribute("type", $row['type']);
  $newnode->setAttribute("time", $row['time']);
} 
echo $dom->saveXML();
?>

这是我运行以下php代码时的错误:

This page contains the following errors:   
error on line 1 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

我认为问题是由"时间"引起的,但我不知道如何解决这个问题。谢谢你的帮助。

在花了近10个小时试图找到错误后,以下是令人痛苦的错误:在打开php标记之前,我忘记了dbinfo.php文件中的空白!!!!

 <?php
$host="localhost";
$username="abc";
$password="abc";
$database="abc";
?>

案件已结案。。。