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

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

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

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)

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

and my tables are setup as follows

[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]

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

[COLOR=black]

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

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

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

[/COLOR]

or

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

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

"    $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]

hi,

sorry was going a bit to fast :wink:

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]

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:

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

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.

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

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.

when checked the source of your webpage:

<!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 ?

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?

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

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.

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

You don’t have an error?

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.

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