Prefiero usar simplexml
, xmlwriter
o DOM
para construir ese XML, y PDO
para el manejo de la base de datos:
$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();
Y una pequeña variante con una matriz de mapeo:
$fields = array(
'singer' => 'artist',
'song' => 'title',
# ...
);
foreach ($requests as $r) {
$cd = $xml->addChild("CD");
foreach ($fields as $name => $column)
{
$cd->$name = $r[$column];
}
}