通过PHP将MySQL表中的数据转换为XML

通过PHP将MySQL表中的数据转换为XML,php,mysql,xml,Php,Mysql,Xml,可能重复: 我正在尝试学习本教程: 谷歌地理API团队; 2009年8月 我已经到了一个位置数据MySQL表的地步。出于我的目的,我在本教程中更改了代码的元素。我只想从MySQL表中提取所有数据,并将其转换为XML,这样它就可以被GoogleMapsAPI读取 不幸的是,当我运行下面的代码时,浏览器页面没有加载并收到以下错误: 警告:无法修改标题信息-标题已由第43行的…/getdata/genxml.php中的…/getdata/genxml.php:8处开始的输出发送 这可能是有问题的一

可能重复:

我正在尝试学习本教程:


谷歌地理API团队; 2009年8月

我已经到了一个位置数据MySQL表的地步。出于我的目的,我在本教程中更改了代码的元素。我只想从MySQL表中提取所有数据,并将其转换为XML,这样它就可以被GoogleMapsAPI读取

不幸的是,当我运行下面的代码时,浏览器页面没有加载并收到以下错误:

警告:无法修改标题信息-标题已由第43行的…/getdata/genxml.php中的…/getdata/genxml.php:8处开始的输出发送

这可能是有问题的一行:

header("Content-type: text/xml");
这是整个代码块。我犯了什么错误?谢谢

<?php
require("phpsqlsearch_dbinfo.php");

//Get parameters from URL
//$center_lat = $_GET["lat"];
//$center_lng = $_GET["lng"];

// 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 (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());
}

// Search the rows in the markers table
$query = sprintf("SELECT * FROM markers WHERE 1");
$result = mysql_query($query);

$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}
// **Below is line 42. The error may be here.** 
header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("id", $row['id']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("bt", $row['bt']);
  $newnode->setAttribute("dt", $row['dt']);
  $newnode->setAttribute("br", $row['br']);
  $newnode->setAttribute("bth", $row['bth']);
  $newnode->setAttribute("sqft", $row['sqft']);
  $newnode->setAttribute("lp", $row['lp']);
  $newnode->setAttribute("url", $row['url']);
  $newnode->setAttribute("dom", $row['dom']);
}
echo $dom->saveXML();

?>
createElement(“标记”);
$parnode=$dom->appendChild($node);
//打开到mySQL服务器的连接
$connection=mysql\u connect(localhost,$username,$password);
如果(!$connection){
die(“未连接:”.mysql_error());
}
//设置活动mySQL数据库
$db\u selected=mysql\u select\u db($database,$connection);
如果(!$db_选中){
die(“无法使用db:.mysql_error());
}
//搜索标记表中的行
$query=sprintf(“从标记中选择*,其中1”);
$result=mysql\u query($query);
$result=mysql\u query($query);
如果(!$result){
die(“无效查询:”.mysql_error());
}
//**下面是第42行。错误可能在此处。**
标题(“内容类型:text/xml”);
//遍历行,为每个行添加XML节点
while($row=@mysql\u fetch\u assoc($result)){
$node=$dom->createElement(“标记”);
$newnode=$parnode->appendChild($node);
$newnode->setAttribute(“id”,$row['id']);
$newnode->setAttribute(“地址”,$row['address']);
$newnode->setAttribute(“lat”,$row['lat']);
$newnode->setAttribute(“lng”,$row['lng']);
$newnode->setAttribute(“bt”,$row['bt']);
$newnode->setAttribute(“dt”,$row['dt']);
$newnode->setAttribute(“br”,$row['br']);
$newnode->setAttribute(“bth”,$row['bth']);
$newnode->setAttribute(“sqft”,$row['sqft']);
$newnode->setAttribute(“lp”,$row['lp']);
$newnode->setAttribute(“url”,$row['url']);
$newnode->setAttribute(“dom”,$row['dom']);
}
echo$dom->saveXML();
?>

删除
之前的所有空格和字符当您已发送要输出的内容,然后尝试调用header()时,会出现您发布的错误


在header()调用之前有东西正在输出,您需要找到它。它可能位于phpsqlsearch_dbinfo.php源文件中,也可能位于
的上方。在该错误为一般服务器错误(500系列)之前,是否确实有两个空格。你能打开erros吗?这样我们就可以看到这个页面抛出的真正错误了?我如何打开错误?抱歉,我是新手:)如果你重新阅读我的评论,你会发现我最初没有注意到header()调用。我甚至在看到header()调用后对其进行了编辑,以反映是的,这肯定会是一个问题。@kingspippus-理论上,最好删除注释的这一部分,以防止“某些”人得出结论<代码>:P
尝试将die()放在header()调用的正上方,然后查看结果页面的源代码。里面有什么?如果没有看到任何内容,请按Ctrl-a键选择页面源中的“全部”,然后按Ctrl-C键并将其粘贴到文本编辑器中,以查看是否有任何无意中漏掉的空白。我已经遇到过好几次了,要找到它通常是件痛苦的事,但我保证,它就在那里的某个地方现在我删除了PHP代码以正确地启用错误返回和代码块函数。谢谢你的建议!很高兴我能帮忙!不过,这是一个很快的建议。即使您没有输出错误返回,您也可能希望将它们记录到文件或其他文件中,以确保它们以某种方式被捕获。是的,它们正在被记录。再次感谢!