Page 1 of 1

PHP and XML

Posted: Thu Jun 25, 2009 6:46 pm
by VarwigDude
I'm trying to Write a XML file with PHP The Code I have is

Code: Select all

 
<?php
 
header("Content-type: text/xml");
 
$myServer = "MYSERVER\SQLEXPRESS";
$myUser = "sa";
$myPass = "MYPW";
$myDB = "cars"; 
 
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 
 
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB"); 
 
//declare the SQL statement that will query the database
$query = "SELECT id, type, year ";
$query .= "FROM cars";
 
//execute the SQL query and return records
$result = mssql_query($query); 
 
//display the results 
while($row = mssql_fetch_array($result))
{
    $yearrow = $row['year'];
    $typerow = $row['type'];
 
    echo "<note>";
        echo "<from>$yearrow</from>";
        echo "<to>$typerow</to>";
    echo "</note>";
 
}
 
//close the connection
mssql_close($dbhandle);
 
?>
 
The Data come out correctly but i get the Error "Only one top level element is allowed in an XML document. Error processing resource" and "Only one top level element is allowed in an XML document. Error processing resource"

Can anyone help!

Thanks Guys

Re: PHP and XML

Posted: Thu Jun 25, 2009 7:24 pm
by requinix
http://en.wikipedia.org/wiki/XML
Read the Well-formedness section - at the very least the first paragraph.

Re: PHP and XML

Posted: Fri Jun 26, 2009 9:59 am
by juma929
Hello,

You havnt stated that its an XML document and the version. To be well formed you need to do this, to be valid you also need a DOC TYPE.

Put the following right at the beginning of your xml file:

Code: Select all

 
echo '<?xml version="1.0"?>';
 
Make sure nothing is sent before the line of code i've gave you there.

Thanks.

Re: PHP and XML

Posted: Fri Jun 26, 2009 5:31 pm
by VarwigDude
echo '<?xml version="1.0"?>';. Worked Thank You :D

Re: PHP and XML

Posted: Sat Jun 27, 2009 11:29 am
by juma929
Hello,

No problem at all, if you need any more help with PHP/XML please let me know and ill do my best :).

Thanks,
Justin