0

I am having a issue with getting some MYSQL data to a XML output. I have done some research and i am not trying to invoke the header before a echo. The below code is from my example_xml.php file Below is the exact error as well. Any help is much appreciated.

  Warning: Cannot modify header information - headers already sent by (output started at     /home/content/59/11513559/html/bg/example_xml.php:2) in /home/content/59/11513559/html/bg/example_xml.php on line 65

line 65 is

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



    <?php

 //database configuration
 $config['mysql_host'] = "localhost";
 $config['mysql_user'] = "placeholder";
 $config['mysql_pass'] = "placeholder";
 $config['db_name']    = "placeholder";
 $config['table_name'] = "placeholder";

 //connect to host
 mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
 //select database
 @mysql_select_db($config['db_name']) or die( "Unable to select database");




 // start creating xml document

 $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
 $root_element = $config['table_name']."s"; //fruits
 $xml         .= "<$root_element>";


 //select all items in table
 $sql = "SELECT * FROM ".$config['table_name'];

 $result = mysql_query($sql);
 if (!$result) {
die('Invalid query: ' . mysql_error());
 }

 if(mysql_num_rows($result)>0)
 {
 while($result_array = mysql_fetch_assoc($result))
 {
  $xml .= "<".$config['table_name'].">";

  //loop through each key,value pair in row
  foreach($result_array as $key => $value)
  {
     //$key holds the table column name
     $xml .= "<$key>";

     //embed the SQL data in a CDATA element to avoid XML entity issues
     $xml .= "<![CDATA[$value]]>"; 

     //and close the element
     $xml .= "</$key>";
  }

  $xml.="</".$config['table_name'].">";
  }
  }



  //close the root element
 $xml .= "</$root_element>";



 //send the xml header to the browser
 header ("Content-Type:text/xml"); 

 //output the XML data
 echo $xml;
 ?>
st15jap
  • 91
  • 1
  • 2
  • 11

1 Answers1

0

Even a single space can cause this error. Remove unnecessary white-space, but you can place this (corrected) header at the top of your script

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

as this header information is not within any conditional statement - it is always sent.

Alternatively, use output buffering; add the following as the very first line:

ob_start();

and at the end,

ob_flush();

See the docs on output buffering.

Andy G
  • 19,232
  • 5
  • 47
  • 69
  • Also note that the mysql extension is deprecated and using @ to suppress error messages is not recommended. – Andy G Aug 25 '13 at 00:41
  • Thanks Andy G for your info. I also removed the @.One more question if you would how can i take the tree and display it in the address bar? Thanks for your help. – st15jap Aug 25 '13 at 00:53
  • You mean the XML-tree? It doesn't usually appear in the address bar - at least, I haven't seen it there. Unless there is a browser-setting that does this then I doubt that it is possible - at least, not with an xml document. (It could be done, sort-of, with an HTML document and lots of messy JavaScript.) Maybe someone else can advise you on this aspect. – Andy G Aug 25 '13 at 01:00
  • ok i appreciate everything you have done. Thanks again Andy! your Beaker picture is awesome by the way – st15jap Aug 25 '13 at 01:02
  • Thank you - that was taken when I had hair :) I +1'd d'alar'cop as he mentioned moving the header() statement to the top before me. – Andy G Aug 25 '13 at 01:04
  • LOL! I didn't know i could do that, but i just did. Thanks as well d'alar'cop! – st15jap Aug 25 '13 at 01:10