0

可能重复:
PHP 已发送的标头

我正在尝试遵循本教程:

使用 PHP、MySQL 和 Google Maps Google Geo API 团队创建商店定位器;
2009 年 8 月

我已经到了拥有 MySQL 位置数据表的地步。为了我的目的,我更改了本教程中的代码元素。我只是想从 MySQL 表中提取所有数据并将其转换为 XML,以便 Google Maps API 可以读取它。

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

警告:无法修改标头信息 - 标头已由第 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();

?>
4

2 回答 2

1

当您已经发送要输出的内容然后尝试发出对 header() 的调用时,就会给出您发布的错误。

在调用 header() 之前,有些东西正在输出,你需要找到它。它可能在 phpsqlsearch_dbinfo.php 源文件中,或者它可能在您的<?php标记之上,这是一个常见的错误。(或者可能在<?php标签上方或?>phpsqlsearch_dbinfo.php 中的标签之后。)

Another thing that I've seen as a problem before is that some editors don't translate newlines and carriage returns correctly, which can be a problem. If you're editing the file in Notepad, use a different editor that supports newlines and carriage returns correctly, such as PSPad (freeware) or UltraEdit (commercial), and make sure that the file is saved in Unix format, not DOS format. Or if you edit it at a Unix command prompt using something like vim, it will be immediately obvious if you have extra carriage returns in your source code.

But like I said, that error is explicitly telling you that something has already been output to the browser when you try to make the header() call. The header() call is only legal when nothing (except possibly other header() calls) has gone before, including any white space or html markup.

于 2012-07-01T06:55:01.327 回答
0

<?php删除标签前的所有空格和字符

于 2012-07-01T05:37:31.943 回答