一部の 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
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;
?>