Wolałbym używać simplexml
, xmlwriter
lub DOM
aby zbudować ten XML i PDO
do obsługi bazy danych:
$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();
I mały wariant z tablicą mapującą:
$fields = array(
'singer' => 'artist',
'song' => 'title',
# ...
);
foreach ($requests as $r) {
$cd = $xml->addChild("CD");
foreach ($fields as $name => $column)
{
$cd->$name = $r[$column];
}
}