| 1 |
<?php |
| 2 |
|
| 3 |
// setting up PDO |
| 4 |
$dbLocation = 'mysql:dbname=db001;host=localhost'; |
| 5 |
$dbUser = 'user'; |
| 6 |
$dbPass = 'password'; |
| 7 |
$db = new PDO($dbLocation, $dbUser, $dbPass); |
| 8 |
|
| 9 |
// prepare all queries... |
| 10 |
$dbArtists = $db->prepare("SELECT * FROM artist"); |
| 11 |
$dbAlbums = $db->prepare("SELECT * FROM album WHERE artist_ID=:artist_id"); |
| 12 |
$dbSongs = $db->prepare("SELECT * FROM song WHERE album_ID=:album_id"); |
| 13 |
|
| 14 |
// fetch all artists |
| 15 |
$dbArtists->execute(); |
| 16 |
$artists=$dbArtists->fetchAll(PDO::FETCH_ASSOC); |
| 17 |
|
| 18 |
|
| 19 |
$x=new XMLWriter(); |
| 20 |
$x->openMemory(); |
| 21 |
$x->startDocument('1.0','UTF-8'); |
| 22 |
$x->startElement('music'); |
| 23 |
|
| 24 |
foreach ($artists as $artist) { |
| 25 |
|
| 26 |
$x->startElement('artist'); |
| 27 |
$x->writeAttribute('name',$artist['artist']); |
| 28 |
|
| 29 |
// fetch all albums of this artist |
| 30 |
$dbAlbums->execute(array(':artist_id' => $artist['artist_id'])); |
| 31 |
$albums = $dbAlbums->fetchAll(PDO::FETCH_ASSOC); |
| 32 |
|
| 33 |
foreach ($albums as $album) { |
| 34 |
|
| 35 |
$x->startElement('album'); |
| 36 |
$x->writeAttribute('name',$album['album']); |
| 37 |
|
| 38 |
// fetch all songs from this album |
| 39 |
$dbSongs->execute(array(':album_id' => $album['album_id'])); |
| 40 |
$songs = $dbSongs->fetchAll(PDO::FETCH_ASSOC); |
| 41 |
|
| 42 |
foreach ($songs as $song) { |
| 43 |
|
| 44 |
$x->startElement('song'); |
| 45 |
$x->text($song['song']); |
| 46 |
$x->endElement(); // song |
| 47 |
} // foreach $songs |
| 48 |
|
| 49 |
$x->endElement(); // album |
| 50 |
} // foreach $albums |
| 51 |
|
| 52 |
$x->endElement(); // artist |
| 53 |
} // foreach $artists |
| 54 |
|
| 55 |
$x->endElement(); // music |
| 56 |
$x->endDocument(); |
| 57 |
$xml = $x->outputMemory(); |