tags:

views:

186

answers:

4

i've created an rss feed in php using the below code. i've double checked all the variables (i.e. username and password) and they are correct. mysql is set up correctly as well. i'm just getting a blank page when i try to view this locally on my computer using mamp. any help would be appreciated.. does this code look correct, and is there something else i should be using to view this other than mamp?

<? header('Content-type: text/xml'); ?>
 
<?php
$dbhost = "localhost"; // almost always localhost.
$dbname = "links"; // Database Name
$dbuser = "root"; // Database Username
$dbpass = "password"; // Databse Password
 
$connect = mysql_connect("$dbhost","$dbuser","$dbpass");// Connecting to Database
mysql_select_db($dbname) or die (mysql_error()); // Selecting Database
?> 
 
<rss version="2.0">
<channel>
    <title> </title>
    <description> </description>
    <link></link>

<?
$sql = "SELECT * FROM news limit 5";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
?>
 
<item>
     <title><?=$row['title']; ?></title>
     <author><?=$row['author']; ?></author>
     <link>http://MYSITE.com/news.php?id=&lt;?=$row['id']; ?></link>
</item>
 
<?
}
?>
 
</channel>
</rss>
+1  A: 

A couple things to try:

  1. Check the logs.
  2. Move the call to header() to the top, before output.
  3. Remove the <blockquote> tags.
  4. Add a processing instruction (<?xml version="1.0"?>) at the top, before the <rss> element.
  5. Change the content-type header from text/xml to application/xml.
Bryan Kyle
kRON
still nothing.. i noticed if i take out the header alltogether i get data back from my table, but most of it is unreadable. so i'm thinking the solution must be something close to what you have above
jwb
A: 

When I copy this code and change around the MySQL settings to use a database that actually exists for my setup of MAMP, this works fine for me, with 0 items since the table doesn't exist in the DB. In other words, it seems like something is wrong with your MAMP setup, not your code. Are you absolutely sure that your MySQL server is running and you have the right information for connecting to it?

Rudd Zwolinski
A: 

In MAMP you can get to the PHP error log by opening the MAMP panel and clicking Server > PHP > View Log. Depending on your setup you may get blank output when an error occurs. The error may help lead you in the right direction.

GloryFish
A: 

I wonder if all your output is getting escaped properly. Does the data in your mysql table include any of the characters that need to be treated specially?

If you have the PHP cli installed you might try using that for testing things. Sometimes you'll see things that you won't see through a web browser or RSS client.

I use feedcreator http://feedcreator.org/ for all my RSS generation under PHP. I generally find it is easier to use a library. I posted an example here.

You might also want to consider disabling short_tags. Because you are actually trying to output XML, you might actually be confusing things. Particularly if you try and send something like '' at the top of your page.

Zoredache