尝试从MYSQL获取数据到XML时出错[重复]

时间:2022-04-02 09:43:53

This question already has an answer here:

这个问题在这里已有答案:

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.

我遇到了将一些MYSQL数据转换为XML输出的问题。我做了一些研究,我不想在回声之前调用标题。下面的代码来自我的example_xml.php文件以下是确切的错误。任何帮助深表感谢。

  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

第65行是

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

1 个解决方案

#1


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.

请参阅有关输出缓冲的文档。

#1


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.

请参阅有关输出缓冲的文档。