douhao5280 2013-05-10 18:53
浏览 7
已采纳

mysql到php到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>
";

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

1条回答 默认 最新

  • dongpang1898 2013-05-10 19:37
    关注

    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];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题