• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1275
  • Last Modified:

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.
0
awarraic
Asked:
awarraic
  • 8
  • 6
1 Solution
 
Ray PaseurCommented:
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 ;-)
0
 
awarraicAuthor Commented:
@Ray, use common sense and let's see if you can find the code first before you can solve this issue.
0
 
awarraicAuthor Commented:
@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;
      }

}

?>

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
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.
0
 
awarraicAuthor Commented:
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.
0
 
Ray PaseurCommented:
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?
0
 
awarraicAuthor Commented:
<?

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>
0
 
Ray PaseurCommented:
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?
0
 
awarraicAuthor Commented:
Yes, I have db_rss with the same columns/tables.
0
 
Ray PaseurCommented:
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).
0
 
Ray PaseurCommented:

<?php // RAY_temp_awarraic.php
error_reporting(E_ALL);
ob_start();

// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES

// GET THE RSS HEADERS
// MAN PAGE:http://php.net/manual/en/function.mysql-query.php
$sql = "SELECT * FROM webref_rss_details LIMIT 1";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS

// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-assoc.php
while ($row = mysql_fetch_assoc($res))
{
    // RSS HEADER IN HEREDOC NOTATION
    extract($row);
    $details = <<<DETAILS
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
  <title>$title</title>
  <link>$link</link>
  <description>$description</description>
  <language>$language</language>
  <image>
    <title>$image_title</title>
    <url>$image_url</url>
    <link>$image_link</link>
    <width>$image_width</width>
    <height>$image_height</height>
  </image>
DETAILS;

    echo $details;
}


// GET THE RSS ITEMS
$sql = "SELECT * FROM webref_rss_items";
$res = mysql_query($sql);
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}

while ($row = mysql_fetch_array($res))
{
    // RSS ITEMS IN HEREDOC NOTATION
    extract($row);
    $items = <<<ITEMS
  <item>
    <title>$title</title>
    <link>$link</link>
    <description><![CDATA[$description]]></description>
  </item>
ITEMS;

    echo $items;
}

echo '</channel>' . PHP_EOL . '</rss>';

// END OF CREATING THE RSS STRING
$rss = ob_get_clean();
header("Content-Type: application/xml; charset=ISO-8859-1");
echo $rss;
die();

Open in new window

0
 
Ray PaseurCommented:
Have you tested the script yet?
0
 
awarraicAuthor Commented:
Yes, I tried the script, didn't work or maybe I missed out something.
0
 
Ray PaseurCommented:
Is that the error message you got, "Didn't work?"
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now