google map api只在文档错误开始时才允许XML声明

时间:2014-04-11 03:21:30

标签: php mysql api google-maps xml-parsing

我在此链接https://developers.google.com/maps/articles/phpsqlajax_v3上使用google maps api并尝试将其用于我的数据库。但是当我运行这个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.

我认为问题是由“时间”造成的,但我无法弄清楚如何解决这个问题。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

花了将近10个小时试图找到错误之后,这就是痛苦的错误: 在打开php标签!!!!

之前,我忘记了dbinfo.php文件中的空白空间
 <?php
$host="localhost";
$username="abc";
$password="abc";
$database="abc";
?>

案件结束......