0

Possible Duplicate:
Headers already sent by PHP

I am attempting to follow this tutorial:

Creating a Store Locator with PHP, MySQL & Google Maps
Google Geo APIs Team; August 2009

I've reached the point where I have a MySQL table of location data. I changed elements of the code in this tutorial for my purposes. I simply would like to extract all the data from the MySQL table and convert it into XML, so it can be read by the Google Maps API.

Unfortunately, when I run the code below, the browser page does not load and receive the following error:

Warning: Cannot modify header information - headers already sent by (output started at .../getdata/genxml.php:8) in .../getdata/genxml.php on line 43

This may be the problematic line:

header("Content-type: text/xml");

Here is the entire block of code. What mistake am I making? Thanks!

<?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();

?>
Community
  • 1
  • 1
AME
  • 5,234
  • 23
  • 71
  • 81
  • Do you really have two spaces before the script. If those spaces really are there, you need to remove them. – King Skippus Jul 01 '12 at 05:25
  • 1
    This error is a generic server error (500 series). Can you turn on the erros so we can see the real error that this page is throwing? – Ricardo Souza Jul 01 '12 at 05:44
  • How do I turn on errors? Sorry, I'm a newbie :) – AME Jul 01 '12 at 05:48
  • http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors and http://php.net/manual/en/function.error-reporting.php and http://www.php.net/manual/en/function.ini-set.php – Ricardo Souza Jul 01 '12 at 05:52
  • Ok, errors are above. Thanks. – AME Jul 01 '12 at 06:31
  • @KingSkippus - "Two spaces before" a `header()` call is *always* a problem. Always. Nothing can be sent to the ouput stream period if you want to set `header()` directives. Even some frameworks omit the closing `?>` on class files and other non-interleaved-with-output files, simply to prevent a stray whitespace right after one of those end tags. – Jared Farrish Jul 01 '12 at 06:48
  • 2
    If you re-read my comment, you'll see that I didn't notice the header() call initially. I even edited it after I saw the header() call to reflect that yes, it would definitely be a problem. – King Skippus Jul 01 '12 at 06:50
  • 1
    @KingSkippus - In theory, it would have been better to remove that part of the comment, so as to prevent "some" people from jumping to conclusions. `:P` – Jared Farrish Jul 01 '12 at 06:51
  • The script runs correctly and outputs the XML exactly as it should, thanks for the help. For bonus points, how can I modify the above code to actually save the XML as an XML file in the same directory? – AME Jul 01 '12 at 07:06

2 Answers2

1

The error you posted is given when you have already sent something to be output and then try to issue a call to header().

Something is outputting something before that header() call, and you need to find it. It might be in the phpsqlsearch_dbinfo.php source file, or it could be above your <?php tag, which is a common mistake. (Or possibly above the <?php tag or after the ?> tag in 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.

King Skippus
  • 3,801
  • 1
  • 24
  • 24
  • Try putting a die() just above the header() call and then view the source of the resulting page. What's in it? If you don't see anything, do a Ctrl-A to select all in the page source, then Ctrl-C and paste it into a text editor to see if there is any unintended whitespace that's slipped through. I've run into this a few times, and it's usually a pain in the butt to track down, but I promise, it's in there somewhere. ;) – King Skippus Jul 01 '12 at 06:58
  • I removed the PHP code to enable error returns and the code block functions correctly now. Thanks for the spot on advice! – AME Jul 01 '12 at 07:01
  • Glad I could help! Quick suggestion, though. Even if you're not outputting the error returns, you might want to log them to a file or something to make sure they're being captured somehow. – King Skippus Jul 01 '12 at 07:05
  • Yes, they are being logged. Thanks again! – AME Jul 01 '12 at 07:07
0

remove any spaces and character before <?php tag

mohammad falahat
  • 757
  • 1
  • 4
  • 11