# Tutorial : Output mySQL data as XML with PHP (problem)

**URL:** <https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302>\
**Category:** programming\
**Created:** [March 25, 2006, 5:40pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302 "2006-03-25T17:40:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rollo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rollo/32/2297_2.png) [@Rollo](https://forum.kirupa.com/u/Rollo)\
**Post date:** [March 25, 2006, 5:40pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/1 "2006-03-25T17:40:08Z")

</div>

hello everyone.

I have spent ages looking for a good php script which can generate an RSS fairly automatically. The tutorial which is located here on Kirupa seems to be exactly what i’m looking for.

I’m having a problem with it thou. When i execute the file in the browser nothing happens. The fields i want to retrive from the database are: title, date and message.  
This is the php code which i am using:

```php

 <?php

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

$host = " ********";
$user = " ********";
$pass = " ********";
$database = " ********";

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

$query = "SELECT * FROM mynews ORDER BY date DESC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\"?>
";
$xml_output .= "<entries>
";

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
    $row = mysql_fetch_assoc($resultID);
    $xml_output .= "	<entry>
";
    $xml_output .= " <title>" . $row['title'] . "</title>
";
    $xml_output .= " <date>" . $row['date'] . "</date>
";
        // Escaping illegal characters
        $row['message'] = str_replace("&", "&", $row['message']);
        $row['message'] = str_replace("<", "<", $row['message']);
        $row['message'] = str_replace(">", ">", $row['message']);
        $row['message'] = str_replace("\"", "&quot;", $row['message']);
    $xml_output .= " <message>" . $row['message'] . "</message>
";
    $xml_output .= "	</entry>
";
}

$xml_output .= "</entries>";

echo $xml_output;

?> 

```

Any assistance is greatly appriciated. Thanks very much and thanks to the writer for a great tutorial too

---

<div class="post-metadata">

**Author:** ![Voccart](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@Voccart](https://forum.kirupa.com/u/Voccart)\
**Post date:** [March 25, 2006, 7:29pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/2 "2006-03-25T19:29:25Z")

</div>

> 

When i execute the file in the browser nothing happens

only getting a blank screen ?

are your tables setup corectly ?  
example:  
title: varchar(255)  
date: datetime  
message: varchar(255)

---

<div class="post-metadata">

**Author:** ![Rollo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rollo/32/2297_2.png) [@Rollo](https://forum.kirupa.com/u/Rollo)\
**Post date:** [March 25, 2006, 8:39pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/3 "2006-03-25T20:39:38Z")

</div>

hi voccart, yup a blank screen  
[http://www.pixelscience.it/rss.php](http://www.pixelscience.it/rss.php)

and my tables are setup as follows

```auto
[COLOR=#99ccff]CREATE TABLE `mynews` (
    `id` int(11) NOT NULL auto_increment,
    `title` varchar(255) NOT NULL default '',
    `user` varchar(22) NOT NULL default '',
    `date` varchar(50) NOT NULL default '0000-00-00',
    `message` text NOT NULL,
    PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;[/COLOR]

```

---

<div class="post-metadata">

**Author:** ![Voccart](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@Voccart](https://forum.kirupa.com/u/Voccart)\
**Post date:** [March 25, 2006, 8:46pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/4 "2006-03-25T20:46:27Z")

</div>

[COLOR=black]try this[/COLOR]  
[COLOR=black][/COLOR]

[COLOR=black]

```auto
`date` varchar(50) NOT NULL default '0000-00-00',

```

[/COLOR]  
[COLOR=black][/COLOR]  
[COLOR=black]change it to [/COLOR]  
[COLOR=black]

```auto
`date` datetime NOT NULL default '0000-00-00 00:00:00',

```

[/COLOR]

or

```auto
`date` date NOT NULL default '0000-00-00',

```

---

<div class="post-metadata">

**Author:** ![Rollo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rollo/32/2297_2.png) [@Rollo](https://forum.kirupa.com/u/Rollo)\
**Post date:** [March 25, 2006, 8:48pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/5 "2006-03-25T20:48:50Z")

</div>

what i’ll do actually instead is…i’ll remove the

```php
" $xml_output .= " <date>" . $row['date'] . "</date>
";"  

```

code from my original file.  
[COLOR=#000000][COLOR=#007700] [COLOR=Black]  
Thanks for the speedy reply btw[/COLOR]  
[/COLOR][/COLOR]

---

<div class="post-metadata">

**Author:** ![Voccart](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@Voccart](https://forum.kirupa.com/u/Voccart)\
**Post date:** [March 25, 2006, 8:56pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/6 "2006-03-25T20:56:33Z")

</div>

hi,

sorry was going a bit to fast 😉

the date column in your myNews table is not setup correctly.

your date column is of a type: VARCHAR  
you better use DATE or DATETIME TYPE COLUMN for saving time/date in your database

i noticed one other thing…  
[COLOR=#99ccff]AUTO\_INCREMENT=1;[/COLOR]  
[COLOR=#99ccff][/COLOR]  
[COLOR=black]did you put data in that table ?[/COLOR]

---

<div class="post-metadata">

**Author:** ![Rollo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rollo/32/2297_2.png) [@Rollo](https://forum.kirupa.com/u/Rollo)\
**Post date:** [March 25, 2006, 8:58pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/7 "2006-03-25T20:58:43Z")

</div>

cool nice one, yeah i knew for a while i had a problem with my date. Never got around to fixing it. Thanks for the info about DATE and DATETIME :pleased:

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [March 25, 2006, 8:59pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/8 "2006-03-25T20:59:50Z")

</div>

That’s not going to fix the main issue though.

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [March 25, 2006, 9:03pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/9 "2006-03-25T21:03:28Z")

</div>

> [@Voccart](#):
>
> i noticed one other thing…  
> [COLOR=#99ccff]AUTO\_INCREMENT=1;[/COLOR]  
> [COLOR=#99ccff][/COLOR]  
> [COLOR=black]did you put data in that table ?[/COLOR]

Doesn’t matter if he did. The for’s going to pull out all the data no matter what the id is.  
edit:/ @rollo: Did you put anything other than the PHP code on the page? There are HTML tags on the page you provided.

---

<div class="post-metadata">

**Author:** ![Rollo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rollo/32/2297_2.png) [@Rollo](https://forum.kirupa.com/u/Rollo)\
**Post date:** [March 25, 2006, 9:06pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/10 "2006-03-25T21:06:12Z")

</div>

hi harish, the only code that is in the file is the php code that i have in my original post, no html what so ever

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [March 25, 2006, 9:19pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/11 "2006-03-25T21:19:29Z")

</div>

I don’t know if this will help… erm at alll… but try using print($xml\_output); rather than the echo. I honestly can’t find a thing wrong with that code, the least it should output is the xml tag.

---

<div class="post-metadata">

**Author:** ![Voccart](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@Voccart](https://forum.kirupa.com/u/Voccart)\
**Post date:** [March 25, 2006, 9:44pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/12 "2006-03-25T21:44:35Z")

</div>

when checked the source of your webpage:

```auto
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY></BODY></HTML>

```

could it be possible that PHP is not compiled with XML support ?

---

<div class="post-metadata">

**Author:** ![Rollo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rollo/32/2297_2.png) [@Rollo](https://forum.kirupa.com/u/Rollo)\
**Post date:** [March 25, 2006, 9:55pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/13 "2006-03-25T21:55:38Z")

</div>

now that you mention it, i did have a previous issue with XMLDOM support with my webhost. They maintain that it is supported but i don’t think it is activated, does the script require xmldom support?

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [March 25, 2006, 10:01pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/14 "2006-03-25T22:01:16Z")

</div>

Try removing the header. (header('content-type…))

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [March 25, 2006, 10:01pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/15 "2006-03-25T22:01:24Z")

</div>

No, that’s not the problem. Check your error log for the error… as your host likely has “display\_errors” set to false in the PHP ini file.

You’re not actually using any xml modules in your script.

As for the html, that’s likely your own browser formatting the blank screen by itself…

---

<div class="post-metadata">

**Author:** ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)\
**Post date:** [March 25, 2006, 10:24pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/16 "2006-03-25T22:24:35Z")

</div>

> [@nokrev](#):
>
> No, that’s not the problem. Check your error log for the error… as your host likely has “display\_errors” set to false in the PHP ini file.
> 
> You’re not actually using any xml modules in your script.
> 
> As for the html, that’s likely your own browser formatting the blank screen by itself…

That’s a possiblity. Are you sure that the table is actually CREATED in your database and that you have the corerct information? If it’s dying and not outputing anything, then it of course won’t be echoing out anything at the end.

---

<div class="post-metadata">

**Author:** ![Rollo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rollo/32/2297_2.png) [@Rollo](https://forum.kirupa.com/u/Rollo)\
**Post date:** [March 25, 2006, 11:12pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/17 "2006-03-25T23:12:58Z")

</div>

proof positive that the db is fully working.  
so far none of the recommendations mentioned here have worked ☹

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [March 25, 2006, 11:20pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/18 "2006-03-25T23:20:52Z")

</div>

You don’t have an error?

---

<div class="post-metadata">

**Author:** ![Rollo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rollo/32/2297_2.png) [@Rollo](https://forum.kirupa.com/u/Rollo)\
**Post date:** [March 26, 2006, 2:01pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/19 "2006-03-26T14:01:08Z")

</div>

no error at all.

_edit_  
i tested the script on another one of my servers and it works perfectly. I must get in contact with my webhost and get them to recompile xmldom on the server.

Sorry about the hassle lads and thanks to those for their assistance.

---

<div class="post-metadata">

**Author:** ![Rollo](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rollo/32/2297_2.png) [@Rollo](https://forum.kirupa.com/u/Rollo)\
**Post date:** [March 26, 2006, 3:47pm UTC](https://forum.kirupa.com/t/tutorial-output-mysql-data-as-xml-with-php-problem/183302/20 "2006-03-26T15:47:16Z")

</div>

actually, one problem. I wanted to use the xml page for an RSS feed. The problem is that the page is rendering as a .php page. Is there any way that i can get it to render as a .xml page ?

the working file is located here  
[http://cormacmoylan.com/test.php](http://cormacmoylan.com/test.php)
