Raději bych použil simplexml
, xmlwriter
nebo DOM
k vytvoření tohoto XML a PDO
pro práci s databází:
$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();
A malá varianta s mapovacím polem:
$fields = array(
'singer' => 'artist',
'song' => 'title',
# ...
);
foreach ($requests as $r) {
$cd = $xml->addChild("CD");
foreach ($fields as $name => $column)
{
$cd->$name = $r[$column];
}
}