dongshan7060 2013-08-25 00:11
浏览 57
已采纳

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

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.

  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;
 ?>
</div>
  • 写回答

1条回答 默认 最新

  • dswm97353 2013-08-25 00:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么