Solved

Creating an RSS feed with PHP/Mysql

Posted on 2011-02-15
14
1,237 Views
Last Modified: 2013-11-18
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
Comment
Question by:awarraic
  • 8
  • 6
14 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34899322
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
 

Author Comment

by:awarraic
ID: 34899524
@Ray, use common sense and let's see if you can find the code first before you can solve this issue.
0
 

Author Comment

by:awarraic
ID: 34899646
@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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34900354
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
 

Author Comment

by:awarraic
ID: 34900527
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34900788
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
 

Author Comment

by:awarraic
ID: 34900856
<?

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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34901328
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
 

Author Comment

by:awarraic
ID: 34901394
Yes, I have db_rss with the same columns/tables.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34901407
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34901722

<?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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34926573
Have you tested the script yet?
0
 

Author Comment

by:awarraic
ID: 34954863
Yes, I tried the script, didn't work or maybe I missed out something.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34955700
Is that the error message you got, "Didn't work?"
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question