Reputation: 119
I have looked for an answer to this question but I have been unable to find an answer that works for my application.
I need to give a response from a sql query in xml format but the response is a url so i will need to use cdata. Here is my code.
if ($_POST['action']=="getSermon")
{
//echo"You got it";
$SQL = "SELECT * FROM sermons";
$allsermon = mysql_query($SQL);
$sermonrow = mysql_fetch_assoc($allsermon);
header('Content-type: text/xml; charset=utf-8');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
$xml = '<?xml version="1.0" encoding="utf-8"?>';
echo $xml;
echo "<SERMON_INFO>";
do {
echo "<SERMON>";
echo "<ID>" . $sermonrow['id'] . "</ID>";
echo "<NAME>" . $sermonrow['Name'] . "</NAME>";
echo "<URL>".<![CDATA[$sermonrow['url']]>."</NAME>";
//echo "<URL>". $sermonrow['url'] . "</URL>";
echo "</SERMON>";
mysql_free_result($result);
} while ($sermonrow = mysql_fetch_assoc($allsermon));
mysql_free_result($allsermon);
echo "</SERMON_INFO>";
mysql_close($db);
}
when i do this i get an error 500 and the page will not load.
Upvotes: 0
Views: 390
Reputation: 12322
The correct syntax is
echo "<URL><![CDATA[".$sermonrow['url']."]]></URL>";
But I'd rather you use an XML library like DOMDocument or SimpleXML to build your document, more complex, but you'll be less prone to simple errors like mixing up a closing tag name (see </NAME>
).
Upvotes: 1