-5

I tried making an RSS feed that loads data from my SQL database.

When I run it, either it takes me to the RSS reader on my internet browser and says "no content", or it will return an error:

This page contains the following errors: error on line 2 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error.

I am using Dreamweaver to make the RSS and am creating it on a PHP file.

<?php require_once('Connections/mydatabase.php'); ?>
<?php 
header('Content-type: text/xml');
$mydatabase = mysqli_connect("http://127.0.0.1","root","au291826","db_cms");
$rss = '<?xml version="1.0" encoding="utf-8"?>';
$rss .= '<rss version="2.0">';
$rss .= '<channel>';
$rss .= '<title>My RSS</title>';
$rss .= '<link>http://localhost/cws_files/</link>';
$rss .= '<description>implementation of RSS with PHP </description>';
$rss .= '<language>ar-sa</language>';
$rss .= '<copyright> RSS </copyright>';
mysql_select_db($database_mydatabase, $mydatabase);
$query_getRecent = "
  SELECT news.post_id, news.title 
  FROM news 
  ORDER BY news.updated 
  DESC LIMIT 10
";
$getRecent = mysql_query($query_getRecent, $mydatabase) or die(mysql_error());
$totalRows_getRecent = mysql_num_rows($getRecent);
while ($row_getRecent = mysql_fetch_assoc($getRecent)){

$rss .= '<item>';
$rss .= '<link> 
  http://localhost/cws_files/index.php?post_id=' . $row_getRecent['post_id'] . '
</link>';
$rss .= '<title>' . $row_getRecent['news.title'] . '</title>';
$rss .= '</item>';

}

$rss .= '</channel>';
$rss .= '</rss>';

echo $rss;
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • You mix `mysql_*` and `mysqli_*`. Take a look at your logfiles. thre should be an error message. – Jens Mar 17 '15 at 09:47
  • Why SHOUTING? And what is the error on line 2? – James Mar 17 '15 at 09:48
  • Hi @Jens, thank you for the quick reply and your input. I've replaced all the mysqli with mysql and the result is still the same. Do you mind telling me where i can locate the log file. – Andrew Udoh Mar 17 '15 at 09:55
  • Hi @James, i am not shouting lol. The error on line 2 happens when i only run the code on google chrome. This is what the error says "This page contains the following errors: error on line 2 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error." – Andrew Udoh Mar 17 '15 at 09:59
  • 1
    @AndrewUdoh You should change to `mysqli_*` because `mysql_* API is deprecated. – Jens Mar 17 '15 at 10:01
  • 3
    @AndrewUdoh Iam not a php programmer. But if you want to be one, you have to know where error messages are displayed. – Jens Mar 17 '15 at 10:03
  • @Jens I've changed the mysql to mysqli but still the same results, nothing shows. I understand that you are not a php, and i appricate you trying to help me. Yes i should know where the error is but then again we are not perfect. – Andrew Udoh Mar 17 '15 at 10:15
  • @AndrewUdoh Can you add your actual code? – Jens Mar 17 '15 at 10:16
  • @Jens the actual rss code is already posted. – Andrew Udoh Mar 17 '15 at 10:24
  • 3
    Not knowing where your PHP error logs are has no relevance to being "perfect" or not. You *need* to access your logs, how else can you know what is going on with PHP and your code? There may be all sorts of issues stopping the code working - e.g. "undefined variable" is just a warning, but might be a reason your code is not working. There are loads of questions on this site to help you locate your error logs - or an internet full of blog articles. Put the initial effort in and you'll find it makes your life *much* easier, and you save so much time. – James Mar 17 '15 at 10:25
  • @AndrewUdoh You should post the code where `mysql_*` is changed to `mysqli_*` – Jens Mar 17 '15 at 10:27

2 Answers2

0

take a look at this line:

$rss .= '<title>' . $row_getRecent['news.title'] . '</title>';

the results array index cannot contain the table name, only the column ones. replace with

 $rss .= '<title>' . $row_getRecent['title'] . '</title>';

and see what you get.

Also this line looks very suspicious.

$rss .= '<link> 
  http://localhost/cws_files/index.php?post_id=' . $row_getRecent['post_id'] . '
</link>';

Check if this is a single line. Otherwise, if there are line breaks, correct this to be a single line.

Igor Moraru
  • 7,089
  • 1
  • 12
  • 24
  • I've tried replacing the code with what you written, but the result is still the same, Nothing shows. – Andrew Udoh Mar 17 '15 at 10:10
  • @AndrewUdoh: inside this while loop, you can use `print_r($row_getRecent)` to see what key values you can use in the array. – halfer Mar 17 '15 at 10:13
  • @halfer. Thank you for your input, unfortunately nothing still happens. – Andrew Udoh Mar 17 '15 at 10:26
  • I updated my answer with new suggestions. check to see if it works. – Igor Moraru Mar 17 '15 at 10:37
  • @IgorMoraru. It's already a single line but noting. I am going to post the modified code that you guys have been helping me to do to make it work. – Andrew Udoh Mar 17 '15 at 10:48
  • No, you've misunderstood @AndrewUdoh - it wasn't a fix - it was to see what fields are available to you. Try it again, and let us know what the output is. Put an `exit()` after it temporarily if you have to. – halfer Mar 17 '15 at 11:25
0
<?php require_once('Connections/mydatabase.php'); ?>
<?php
//header ("Content-Type:text/xml");
?>
<?php

header('Content-type: text/xml');
$mydatabase = mysqli_connect("http://127.0.0.1","root","au291826","db_cms");
$rss = '<?xml version="1.0" encoding="utf-8"?>';
$rss .= '<rss version="2.0">';
$rss .= '<channel>';
$rss .= '<title>My RSS</title>';
$rss .= '<link>http://localhost/cws_files/</link>';
$rss .= '<description>implementation of RSS with PHP </description>';
$rss .= '<language>ar-sa</language>';
$rss .= '<copyright> RSS </copyright>';
mysqli_select_db($database_mydatabase, $mydatabase);
$query_getRecent = "SELECT news.post_id, news.title FROM news ORDER BY news.updated DESC LIMIT 10";
$getRecent = mysqli_query($query_getRecent, $mydatabase) or die(mysqli_error());
$totalRows_getRecent = mysqli_num_rows($getRecent);
while ($row_getRecent = mysqli_fetch_assoc($getRecent)){
$rss .= print_r($row_getRecent);
$rss .= '<item>';
$rss .= '<link> http://localhost/cws_files/index.php?post_id=' . $row_getRecent['post_id'].'</link>';
 $rss .= '<title>' . $row_getRecent['title'] . '</title>';
$rss .= '</item>';

}

$rss .= '</channel>';
$rss .= '</rss>';

echo $rss;
?>