Jump to content

PHP "include" and XML


Jiraiya-Sama

Recommended Posts

Hello Hi,I am trying to generate a XML from PHP and Mysql.This is my code below.

<?phpheader("Content-Type: text/xml");echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";include 'db.php';$sql = "select num from znumrange";$result = mysql_query($sql);$doc = new DomDocument('1.0');echo"<root>";while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {	echo "<num>".$row["num"]."</num>";}echo "</root>";?>

i include another php file (db.php) which contains information such as the database loginhowever, the browser keeps complaining that there is a space in front of <result>. after lots of trial and error, i found that as long as i remove the include 'db.php' and hard code the database login information, the browser display the xml correctly.may i ask if this is really the case? i mean, so i need to hard code all database login information in all those php files?please advice. thanks a lot.

Link to comment
Share on other sites

If db.php contains any characters that will be output, then they will be printed after the <?xml> tag. Output refers to anything that is echoed, but also any spaces, tabs, or linebreaks that are outside the <?php ?> tags. That is probably what is happening here.Is there a reason to include db.php AFTER you echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>" ? Could you include it before your header statement?

Link to comment
Share on other sites

Thanks for your reply.my db.php is just like this actually

<?php$mysql_hostname = "127.0.0.1:3306";$mysql_user = "user";$mysql_password = "123123";$mysql_database = "myDb";$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);mysql_select_db($mysql_database, $bd);?>

there shouldn't be any blank spaces or tab anywhere.I try to put the include statement before or after echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>", but still the same error :)may i ask if there is any way that i can check if there is any spaces in my db.php?thanks a lot.

Link to comment
Share on other sites

Oh... found a solution...i put echo "<root>" before include db.php... and it works .... interesting. thanks for your help Deirdre's Dad :)

<?phpheader("Content-Type: text/xml");echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";echo"<root>";include 'db.php';$sql = "select num from znumrange";$result = mysql_query($sql);$doc = new DomDocument('1.0');while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {	echo "<num>".$row["num"]."</num>";}echo "</root>";?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...