简体   繁体   中英

display mysql query results in xml format using php

I am new to programming and am having trouble trying to render mysql query results in xml format using php. I have looked over my code several time and tried several things but I get a message saying "error on line 2 at column 1: Extra content at the end of the document" in my browser. I have the code below:

<?php
    header ("content-type: text/xml");
    include("database.php");
       $xml='<?xml version="1.0" encoding="UTF-8"?>';
       $res=$pdo->query('SELECT * FROM sk_courses ORDER BY courseID ASC');;
       $xml.='<courses>';
       while ($result=$res->fetch(PDO::FETCH_ASSOC)){
       $xml.='<course>
                <courseID>'.$res['courseID'].'</courseID>
                <courseName>'.$res['courseName'].'</courseName>
                </course>';
    } 
    $xml.='</courses>';
    echo $xml;
?>

You made mistake in variable name ( $result['courseID'] not $res['courseID'] ).

<?php
    header ("content-type: text/xml");
    include("database.php");
       $xml='<?xml version="1.0" encoding="UTF-8"?>';
       $res=$pdo->query('SELECT * FROM sk_courses ORDER BY courseID ASC');;
       $xml.='<courses>';
       while ($result=$res->fetch(PDO::FETCH_ASSOC)){
       $xml.='<course>
                <courseID>'.$result['courseID'].'</courseID>
                <courseName>'.$result['courseName'].'</courseName>
                </course>';
    } 
    $xml.='</courses>';
    echo $xml;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM