I have a problem with outputting a list of a requested song title, artist, album, together with the name of the requester and his message in XML.
I tried this but it gives me a blank page:
$link = mysql_connect("localhost","dbuser","dbpass");
mysql_select_db("dbname");
$query = "SELECT requestlist.host, requestlist.msg, requestlist.name, songlist.ID, songlist.count_requested, songlist.artist, songlist.title,
songlist.album, songlist.buycd, songlist.date_played, count(*) as cnt FROM requestlist, songlist
WHERE (requestlist.songID = songlist.ID) AND (requestlist.code<700) GROUP BY requestlist.msg, songlist.ID, songlist.count_requested,
songlist.artist, songlist.title, songlist.album, songlist.buycd, songlist.date_played ORDER BY requestlist.msg DESC, songlist.date_played DESC" ;
$results = mysql_query($query);
echo "<request>\n";
while($line = mysql_fetch_assoc($results)) {
echo " <CD>\n";
echo "<singer>" . $line["artist"] . "</singer>\n";
echo "<song>" . $line["title"] . "</song>\n";
echo "<album>". $line["album"] ."</album>\n";
echo "<name>". $line["name"] ."</name>\n";
echo "<shout>" . $line["msg"] . "</shout>\n";
echo "</CD>\n";
}
echo "</request>\n";
mysql_close($link);
I'd rather use simplexml
, xmlwriter
or DOM
to build that XML, and PDO
for database handling:
$stmt = $db->prepare($query); // define $db as PDO in include-file
$stmt->execute();
$requests = $stmt->fetchAll(PDO::FETCH_ASSOC);
$xml = new SimpleXMLElement("<?xml version="1.0" encoding="UTF-8"?><request></request>");
foreach ($requests as $r) {
$cd = $xml->addChild("CD");
$cd->addchild("singer", $r['artist']);
$cd->addChild("song", $r['title']);
$cd->addChild("album", $r['album']);
$cd->addChild("name", $r['name']);
$cd->addChild("shout", $r['msg']);
}
echo $xml->asXML();
And a small variant with a mapping array:
$fields = array(
'singer' => 'artist',
'song' => 'title',
# ...
);
foreach ($requests as $r) {
$cd = $xml->addChild("CD");
foreach ($fields as $name => $column)
{
$cd->$name = $r[$column];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.