Log in

View Full Version : php xml problem



jamiller
05-23-2007, 08:40 PM
So I am using php to create a xml document based on data in a MySql database. But for some reason when I view the xml.php doc on my website the browser doesn't recognize it should be read as an xml doc and I get this error:

Warning: Cannot modify header information - headers already sent by (output started at blah/xml.php:1) in blah/xml.php on line 3

I replaced the doc structure of my site with blah.

Here is the php code:


<?php

header("Content-type: text/xml");

$dbName = "dbname";
$link = mysql_connect("mydb", "user", "pass") or die("Could not connect.");

$db = mysql_select_db($dbName, $link);

$query = "select * from item order by id ";

$result = mysql_query($query, $link) or die("Could not complete database query");

$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
$_xml .="<portfolio>\r\n";

for($x=0; $x<mysql_num_rows($result); $x++) {
$row = mysql_fetch_assoc($result);
$_xml .="\t<item link=\"" . $row["link"] . "\">\r\n";
$_xml .="\t<title>" . $row["title"] . "</title>\r\n";
$_xml .="\t<descrip>" . $row["description"] . "</descrip>\r\n";
$_xml .="\t\t<img>" . $row["img"] . "</img>\r\n";
$_xml .="\t</item>\r\n";
}

$_xml .="</portfolio>";

echo $_xml;

?>


It's funny too because when I view the source code of the xml.php file it looks exactly like an xml doc with xml version and everything.

jamiller
05-23-2007, 09:29 PM
Ha! **** I fixed it :)

I had whitespace before and after the starting/closing php tags <? php and ?>

Whoduthunkit.

tech_support
05-24-2007, 03:39 AM
Whoduthunkit.
??? :confused:

codeexploiter
05-24-2007, 03:50 AM
I had whitespace before and after the starting/closing php tags <? php and ?>

You can use more simple PHP tags


<?

//Place your PHP code within the start and end tag

?>

mbrodin
05-24-2007, 02:49 PM
You can use more simple PHP tags


<?

//Place your PHP code within the start and end tag

?>


Hi!

You should never use short php-tag "<?" use instead only "<?php" becuase Parsing short tags makes it impossible to print normal XML documents with inline because PHP would interpret this header as a block and will attempt to execute it!

Best regards,
mbrodin

jamiller
05-24-2007, 07:52 PM
??? :confused:

haha :)