-
PHP, XML and mySQL
I have the following code that i am not able to get media:content to work.
PHP Code:
<?php
header("Content-type: text/xml");
$host = "localhost";
$user = "user";
$pass = "IhaveThEanswer2012@!";
$database = "bbl_article";
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$query = "SELECT * FROM links";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml version=\"1.0\" ?>\n";
$xml_output .= "<channel>\n";
$xml_output .= "\t\t<title>Ellucid Movies</title>\n";
$xml_output .= "\t\t<link>http://comingsoon.com</link>\n";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<item>\n";
$xml_output .= "<title>" . $row['name'] .".m4v" . "</title>\n";
$xml_output .= "<link>" . $row['link'] . "</link>\n";
$xml_output .= "<media:content url='" . $row['local_link'] . ".m4v" . "' type='video/mp4'/>\n";
$xml_output .= "\t</item>\n";
}
$xml_output .= "</channel>";
echo $xml_output;
?>
What am I doing wrong.
Also how can I do this in DOM?
I was able to find this
PHP Code:
function createUserDetailsXml(array $result) {
$dom = new DOMDocument;
$dom->formatOutput = TRUE; // enable automatic indenting
$dom->loadXML('<users/>'); // set root node
foreach($result as $row) {
// create user-details node
$user = $dom->createElement('user-details');
// create and append details to user-details node
$user->appendChild(
$dom->createElement('user-id', $row['uid']));
$user->appendChild(
$dom->createElement('user-name', $row['userName']));
$user->appendChild(
$dom->createElement('user-points', $row['points']));
$user->appendChild(
$dom->createElement('image-url', $row['imageURL']));
$user->appendChild(
$dom->createElement('thumb-url', $row['thumbURL']));
// add user-details node to XML document, e.g. users node
$dom->documentElement->appendChild($user);
};
return $dom->saveXML(); // returns the formatted XML
};
But can get it to work.
Thanks for your help in advance.
-
Re: PHP, XML and mySQL
For one...you're iterating the database rows incorrectly.
PHP Code:
while ($row = mysql_fetch_assoc($resultID)) {
...
}