php - XML解析错误: junk after document element__wampserver

标签 php mysql wampserver

我尝试从数据库创建 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)

最佳答案

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

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

关于php - XML解析错误: junk after document element__wampserver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18303035/

相关文章:

php - 使用keyby集合方法检索eloquent api资源

php - 既然 PEAR 方法不再可行,如何为 WAMP 安装 PhpUnit?

windows - 在不询问管理员密码的情况下执行 wamp (windows)

php - 在 PostgreSQL 中使用单引号动态插入文本

javascript - 在 Blade foreach 循环中使用 JS 动态更改 div CSS 背景

php - PHP脚本结束没有任何错误

mysql - 基于 ORDER 字段而不是首次出现的累积字段

java - 实现 UUID 以获得更好的性能

php - 将 API 中的多个值插入 mysql 数据库

mysql - MYSQL 5.5.24 中的 MATCH 不起作用