# Xml from mysql with php

**URL:** <https://forum.kirupa.com/t/xml-from-mysql-with-php/165436>\
**Category:** Uncategorized\
**Created:** [October 11, 2005, 4:25am UTC](https://forum.kirupa.com/t/xml-from-mysql-with-php/165436 "2005-10-11T04:25:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![epix](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@epix](https://forum.kirupa.com/u/epix)\
**Post date:** [October 11, 2005, 4:25am UTC](https://forum.kirupa.com/t/xml-from-mysql-with-php/165436/1 "2005-10-11T04:25:58Z")

</div>

I’m trying to get the output mysql as xml using php tutorial to work, but I’m having one problem with it.

since a php file starts off as \<?php…and ends as ?\> when ever I’m declaring that the output is gonna be xml, like this:

```php

$xml_output = &quot;<?xml version=\&quot;1.0\&quot; encoding=\&quot;iso-8859-1\&quot;?>
&quot;;

```

it misunderstands the "?\>" in the end of the xml declaration as the closing php tag.

here’s all of php code:

```php

<?php

header(&quot;Content-type: text/xml&quot;);

$host = &quot;localhost&quot;;
$user = &quot;root&quot;;
$pass = &quot;pass&quot;;
$database = &quot;wgshop&quot;;

$linkID = mysql_connect($host, $user, $pass) or die(&quot;Could not connect to hose.&quot;);
mysql_select_db($database, $linkID) or die(&quot;Could not find database.&quot;);

$query = &quot;SELECT * FROM shopproducts ORDER BY id DESC&quot;;
$resultID = mysql_query($query, $linkID) or die(&quot;Data not found.&quot;);

$xml_output = &quot;<?xml version=\&quot;1.0\&quot;?>
&quot;;
$xml_output .= &quot;<store>
&quot;;

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++) {
   $row = mysql_fetch_assoc($resultID);
   $xml_output .= &quot;	<products>
&quot;;
	  //Escaping illegal characters
	  $row['prodName'] = str_replace(&quot;&, &quot;&amp;&quot;, $row['prodName']);
	  $row['prodName'] = str_replace(&quot;<&quot;, &quot;&lt;&quot;. $row['prodName']);
	  $row['prodName'] = str_replace(&quot;>&quot;, &quot;&gt;&quot;, $row['prodName']);
	  $row['prodName'] = str_replace(&quot;\&quot;&quot;, &quot;&quot;&quot;, $row['prodName']);
   $xml_output .= &quot; <prodName>&quot; . $row['prodName'] . &quot;</prodName>
&quot;;

	  //Escaping illegal characters
	  $row['price'] = str_replace(&quot;&, &quot;&amp;&quot;, $row['price']);
	  $row['price'] = str_replace(&quot;<&quot;, &quot;&lt;&quot;. $row['price']);
	  $row['price'] = str_replace(&quot;>&quot;, &quot;&gt;&quot;, $row['price']);
	  $row['price'] = str_replace(&quot;\&quot;&quot;, &quot;&quot;&quot;, $row['price']);
   $xml_output .= &quot; <price>&quot; . $row['price'] . &quot;</price>
&quot;;

   $xml_output .= &quot; <section>&quot; . $row['section'] . &quot;</section>
&quot;;

   $xml_output .= &quot; <prodImage>&quot; . $row['prodImage'] . &quot;</prodImage>
&quot;;

	  //Escaping illegal characters
	  $row['description'] = str_replace(&quot;&, &quot;&amp;&quot;, $row['description']);
	  $row['description'] = str_replace(&quot;<&quot;, &quot;&lt;&quot;. $row['description']);
	  $row['description'] = str_replace(&quot;>&quot;, &quot;&gt;&quot;, $row['description']);
	  $row['description'] = str_replace(&quot;\&quot;&quot;, &quot;&quot;&quot;, $row['description']);
   $xml_output .= &quot; <description>&quot; . $row['description'] . &quot;</description>
&quot;;
   $xml_output .= &quot;	</products>
&quot;;
}

$xml_output .= &quot;</store>&quot;;

echo $xml_output;

?>

```

I know that it’s that “?\>” in the xml declaration that messing it all up, but I’m a php novice so I have no clue what to do about it.

I’ve searched the boards and noone else seems to be having this problem.
