本文介绍了mysql到php到xml显示空白年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with outputting a list of a requested song title, artist, album, together with the name of the requester and his message in XML.

我尝试过此方法,但它给了我空白页:

I tried this but it gives me a blank page:

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

推荐答案

我宁愿使用simplexmlxmlwriterDOM构建该XML,并使用PDO进行数据库处理:

I'd rather use simplexml, xmlwriteror DOMto build that XML, and PDOfor database handling:

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

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

And a small variant with a 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];
    }
}

这篇关于mysql到php到xml显示空白年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 04:15