0

我试图从数据库创建 XML,但是当我在 WampServer 中运行我的 PHP 代码时出现错误:

XML Parsing Error: junk after document element
Line Number 2, Column 1:<font size='1'><table class='xdebug-error' 
dir='ltr' border='1' cellspacing='0' cellpadding='1'>

我来自 code.google 的代码:

{<?php

require("phpsqlajax_dbinfo.php");    
// Start XML file, create parent node    
$doc = new DOMDocument('1.0', 'iso-8859-1');    
$node = $doc->createElement("place");    
$parnode = $doc->appendChild($node);

// Opens a connection to a MySQL server    
$connection=mysql_connect (localhost, $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 place table    
$query = "SELECT * FROM place 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 = $doc->createElement("place");    
  $newnode = $parnode->appendChild($node);    
  $newnode->set_attribute("place_id", $row['place_id']);    
  $newnode->set_attribute("p_name", $row['p_name']);    
  $newnode->set_attribute("lat", $row['lat']);    
  $newnode->set_attribute("lng", $row['lng']);    
  $newnode->set_attribute("addr", $row['addr']);    
  $newnode->set_attribute("tel", $row['tel']);    
  $newnode->set_attribute("category", $row['category']);    
  $newnode->set_attribute("description", $row['description']);
}

$xmlfile = $doc->dump_mem();    
echo $xmlfile;

?>

这是表:

place_id(int),    
p_name(varchar),    
lat(float),    
lng(float),    
addr(varchar),    
tel(int),    
category(varchar),    
description(varchar)
4

2 回答 2

0

我对谷歌地图 php/mysql 教程有同样的问题。发现如果我在 localhost ex $connection=mysql_connect ('localhost', $username, $password); 上加上单引号,它会起作用

于 2013-10-01T12:16:02.863 回答
0

您的代码正在生成错误。正如消息所述xdebug-error,这表明您在某处放置了错误,而不是有效代码。

尝试将您的 XML 保存到某个文件或评论您header("Content-type: text/xml");以查看发生了什么。

于 2013-08-18T20:19:45.810 回答