Link to home
Start Free TrialLog in
Avatar of awarraic
awarraicFlag for United States of America

asked on

Creating an RSS feed with PHP/Mysql

I am trying to create an RSS feed. I am following this example at http://www.webreference.com/authoring/languages/xml/rss/custom_feeds/

I am getting this error:

XML Parsing Error: not well-formed
Location: http://localhost/rss/
Line Number 1, Column 3:<?
--^

Not sure what's going on.

Thanks.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Uhh, how would you expect us to help?  Can you post the code that you are using and the RSS feed that it generated?  We are experts but not mind-readers ;-)
Avatar of awarraic

ASKER

@Ray, use common sense and let's see if you can find the code first before you can solve this issue.
@Ray, by the way, I loved this web site, always been very helpful and encouraging attitude. But, lately, it seems like bunch of people sitting and 'll just google and fight with each other and 'll try to accumulate some points instead of helping someone. Sorry, didn't mean to be rude/mean but just come constructive thought/feedback. As far as code, here's the code if you didn't find it on the link that I provided:

Index.php:
-------------
<?php
      header("Content-Type: application/xml; charset=ISO-8859-1");
      include("classes/RSS.class.php");
      $rss = new RSS();
      echo $rss->GetFeed();
?>

RSS.class.php
----------------
<?php

echo "test";
class RSS
{
      public function RSS()
      {
            require_once ("mysql_connect.php");
      }
      
      public function GetFeed()
      {
            return $this->getDetails() . $this->getItems();
      }
      
      private function dbConnect()
      {
            DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
      }
      
      private function getDetails()
      {
            $detailsTable = "webref_rss_details";
            $this->dbConnect($detailsTable);
            $query = "SELECT * FROM ". $detailsTable;
            $result = mysql_db_query (DB_NAME, $query, LINK);
            
            while($row = mysql_fetch_array($result))
            {
                  $details = '<?php xml version="1.0" encoding="ISO-8859-1" ?>
                              <rss version="2.0">
                                    <channel>
                                          <title>'. $row['title'] .'</title>
                                          <link>'. $row['link'] .'</link>
                                          <description>'. $row['description'] .'</description>
                                          <language>'. $row['language'] .'</language>
                                          <image>
                                                <title>'. $row['image_title'] .'</title>
                                                <url>'. $row['image_url'] .'</url>
                                                <link>'. $row['image_link'] .'</link>
                                                <width>'. $row['image_width'] .'</width>
                                                <height>'. $row['image_height'] .'</height>
                                          </image>';
            }
            return $details;
      }
      
      private function getItems()
      {
            $itemsTable = "webref_rss_items";
            $this->dbConnect($itemsTable);
            $query = "SELECT * FROM ". $itemsTable;
            $result = mysql_db_query (DB_NAME, $query, LINK);
            $items = '';
            while($row = mysql_fetch_array($result))
            {
                  $items .= '<item>
                                     <title>'. $row["title"] .'</title>
                                     <link>'. $row["link"] .'</link>
                                     <description><![CDATA['. $row["description"] .']]></description>
                               </item>';
            }
            $items .= '</channel>
                         </rss>';
            return $items;
      }

}

?>

Thanks for posting that.  It looks like something from a PHP 4 time-warp, and it is probably not worth relying on old code like that.  Example: No class constructor.  And it uses this deprecated function (wrongly, I might add) http://us2.php.net/manual/en/function.mysql-db-query.php.  The query could have failed, and you would never know it - the script would just create garbage output.

Can you please post the output you got from running that (the XML from index.php)?  Thanks.
Output from index.php is:
-------------------------------
XML Parsing Error: not well-formed
Location: http://localhost/rss/
Line Number 1, Column 3:<?
--^

Code for index.php
----------------------
<?php
      header("Content-Type: application/xml; charset=ISO-8859-1");
      include("classes/RSS.class.php");
      $rss = new RSS();
      echo $rss->GetFeed();
?>

I think you are right, it's seems really old code. I had to add "php" along with <? to render it properly, not sure what else is broken in there.
Do you know of any other good examples where I can create a RSS feed using mysql/php?

Thanks.
Please do a "view source" on the output from index.php and show us what that looks like.  There should be some XML statements.  I'd like to see what that looks like.

Is your data base set up with these named columns in the two tables referenced here?
<?

DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'xxxxx');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'db_rss');

// Make the connnection and then select the database.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );

?><br />
<b>Notice</b>:  Use of undefined constant DB_HOST - assumed 'DB_HOST' in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />
<br />
<b>Notice</b>:  Use of undefined constant DB_USER - assumed 'DB_USER' in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />
<br />

<b>Notice</b>:  Use of undefined constant DB_PASSWORD - assumed 'DB_PASSWORD' in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: php_network_getaddresses: getaddrinfo failed: No such host is known.  in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />

<br />
<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is kn (trying to connect via tcp://DB_HOST:3306) in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: php_network_getaddresses: getaddrinfo failed: No such host is known.  in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />

<br />
<b>Notice</b>:  Use of undefined constant DB_NAME - assumed 'DB_NAME' in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>25</b><br />
<br />
<b>Warning</b>:  mysql_db_query() expects parameter 3 to be resource, boolean given in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>25</b><br />
<br />

<b>Warning</b>:  mysql_fetch_array() expects parameter 1 to be resource, null given in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>27</b><br />
<br />
<b>Notice</b>:  Undefined variable: details in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>44</b><br />
<br />
<b>Notice</b>:  Use of undefined constant DB_HOST - assumed 'DB_HOST' in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />

<br />
<b>Notice</b>:  Use of undefined constant DB_USER - assumed 'DB_USER' in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />
<br />
<b>Notice</b>:  Use of undefined constant DB_PASSWORD - assumed 'DB_PASSWORD' in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />
<br />

<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: php_network_getaddresses: getaddrinfo failed: No such host is known.  in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is kn (trying to connect via tcp://DB_HOST:3306) in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />

<br />
<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: php_network_getaddresses: getaddrinfo failed: No such host is known.  in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />
<br />
<b>Notice</b>:  Constant LINK already defined in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>17</b><br />

<br />
<b>Notice</b>:  Use of undefined constant DB_NAME - assumed 'DB_NAME' in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>52</b><br />
<br />
<b>Warning</b>:  mysql_db_query() expects parameter 3 to be resource, boolean given in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>52</b><br />
<br />

<b>Warning</b>:  mysql_fetch_array() expects parameter 1 to be resource, null given in <b>C:\wamp\www\rss\classes\RSS.class.php</b> on line <b>54</b><br />
</channel>
                         </rss>
Yeah.  Looks like that script isn't worth much.  Is your data base set up with these named columns in the two tables referenced here?
Yes, I have db_rss with the same columns/tables.
That's good news.  Then we can probably use at least a part of that script, if we bring it into the 21st Century.  I'll see what I can do with it (but I cannot test - you will have to do that part).
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you tested the script yet?
Yes, I tried the script, didn't work or maybe I missed out something.
Is that the error message you got, "Didn't work?"