Jeg vil hellere bruge simplexml
, xmlwriter
eller DOM
at bygge den XML og PDO
til databasehåndtering:
$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();
Og en lille variant med en 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];
}
}