1

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;
?>
  • Possible duplicate of [How to generate XML file dynamically using PHP?](http://stackoverflow.com/questions/486757/how-to-generate-xml-file-dynamically-using-php) – Archish Oct 18 '16 at 03:31

1 Answers1

0

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;
?>
Naga
  • 2,190
  • 3
  • 16
  • 21