0

我在输出请求的歌曲名称、艺术家、专辑列表以及请求者的姓名和他的 XML 消息时遇到问题。

我试过了,但它给了我一个空白页:

$link = mysql_connect("localhost","dbuser","dbpass");
mysql_select_db("dbname");
$query = "SELECT requestlist.host, requestlist.msg, requestlist.name, songlist.ID, songlist.count_requested, songlist.artist, songlist.title,
    songlist.album, songlist.buycd, songlist.date_played,                          count(*) as cnt                         FROM requestlist, songlist
    WHERE   (requestlist.songID = songlist.ID) AND  (requestlist.code<700)    GROUP BY requestlist.msg, songlist.ID, songlist.count_requested,
    songlist.artist, songlist.title, songlist.album, songlist.buycd, songlist.date_played    ORDER BY requestlist.msg DESC, songlist.date_played DESC" ;
$results = mysql_query($query);
echo "<request>\n";

while($line = mysql_fetch_assoc($results)) {
    echo "         <CD>\n";
    echo "<singer>" . $line["artist"] . "</singer>\n";
    echo "<song>" . $line["title"] . "</song>\n";
    echo "<album>". $line["album"] ."</album>\n";
    echo "<name>". $line["name"] ."</name>\n";
    echo  "<shout>" . $line["msg"] . "</shout>\n";
    echo "</CD>\n";   
}
echo "</request>\n";
mysql_close($link);
4

1 回答 1

3

我宁愿使用simplexml,xmlwriterDOM构建该 XML,并PDO用于数据库处理:

$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();

还有一个带有映射数组的小变体:

$fields = array(
    'singer' => 'artist',
    'song'   => 'title',
    # ...
);

foreach ($requests as $r) {
    $cd = $xml->addChild("CD");
    foreach ($fields as $name => $column)
    {
        $cd->$name = $r[$column];
    }
}
于 2013-05-10T19:37:08.017 回答