【问题标题】:google map api getting XML declaration allowed only at the start of the document errorgoogle map api 获取 XML 声明仅在文档错误开始时允许
【发布时间】:2014-04-11 03:21:30
【问题描述】:

我在此链接https://developers.google.com/maps/articles/phpsqlajax_v3 上使用谷歌地图 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.

我认为是“时间”引起的问题,但我不知道如何解决这个问题。 感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

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

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

    结案...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      相关资源
      最近更新 更多