[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Creating an RSS feed with PHP/Mysql

Posted on 2011-02-15
14
Medium Priority
?
1,262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 111

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
Independent Software Vendors: 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!

 
LVL 111

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 111

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 111

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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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 111

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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).
Suggested Courses

650 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