Je préfère utiliser simplexml
, xmlwriter
ou DOM
pour construire ce XML, et PDO
pour la gestion de la base de données :
$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();
Et une petite variante avec un tableau de mapping :
$fields = array(
'singer' => 'artist',
'song' => 'title',
# ...
);
foreach ($requests as $r) {
$cd = $xml->addChild("CD");
foreach ($fields as $name => $column)
{
$cd->$name = $r[$column];
}
}