RSS with php and mysql

Discussion in 'PHP' started by megler, Mar 2, 2008.

  1. #1
    I am trying to work a script where I can create an RSS feed with data pulled from a mysql database.

    Here is the script I am working on:

    <?
    
    class RSS
    {
    	public function RSS()
    	{
    		require_once ('http://mydomain.com/rss/classes/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 = '<?xml version="1.0" encoding="ISO-8859-1" ?>
    					<rss_version>'. $row['rss_version'] .'</rss_version>
    						<channel>'. $row['channel'] .'</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;
    	}
    
    }
    
    ?>
    PHP:
    The problem is when I run it, i am generating this error:

    Cannot view XML input using XSL style sheet.
    Only one top level element is allowed in an XML document. Error processing resource 'http://www.mydomain.com/rss/in...

    <b>Parse error</b>: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' i...


    I have tried removing various '}' per the parse error, but no luck. I am just not sure what is throwing this particular error. Any ideas?

    Right now, I just want something simple that will connect to the database, pull the data and parse it into a RSS feed. I'll worry about extra coding like striping out HTML after I get this part working. ;)

    thanks all

    Marceia
     
    megler, Mar 2, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    require/include of remote stuff is slow and a security risk.
    If it's on your server, why not just do require_once('../../class.php'); or whatever the path is.
    If you're doing it OOP, why not use mysqli?
    In your while loop you want $details .= , not $details =
    Also, using single quotes is faster than double quotes;)
    What line does it say the error is occuring on?
     
    matthewrobertbell, Mar 2, 2008 IP
  3. megler

    megler Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You know, i'm never sure if I'm supposed to put an absolute path or a URL. This script is on my hosted server - so what's best? God, that's such a stupid question on my part, I know. bah.

    You got me on that one, can you further clarify?

    I'll take care of those errors now. You know, I thought about that when I noticed the script had single quotes in one spot and doubles in another, but figured the programmer had a reason for it. I'll clean that up.

    The error line is cut off - ugh. What I have above is exactly what I'm seeing in my browser. Is there any way to generate the entire error line?

    Matthew, thanks for your help. I'll keep plugging away and look forward to your reply. :)
     
    megler, Mar 2, 2008 IP
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4
    Path is always better. I also think there is less server load using path.

    The rest of that stuff is beyond me also so don't feel bad.
     
    Colbyt, Mar 2, 2008 IP
  5. megler

    megler Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks, Colbyt, always good to know I'm not alone!

    ok, I've fixed all of the code except for the parse error, because not sure what line number it's on. here's the current code:

    
    <?
    
    class RSS
    {
    	public function RSS()
    	{
    		require_once (/pathto/rss/classes/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 .= '<?xml version="1.0" encoding="ISO-8859-1" ?>
    					<rss_version>'. $row['rss_version'] .'</rss_version>
    						<channel>'. $row['channel'] .'</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;
    	}
    
    }
    
    ?>
    
    PHP:
     
    megler, Mar 2, 2008 IP
  6. megler

    megler Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok, I have a working script for anyone that's interested:

    <?php   
    include('../rss/classes/mysql_connect.php');   
    header('Content-type: application/rss+xml; charset=utf-8');   
    echo "<?xml version=\"1.0\" ?>";   
    echo "   
    <rss version=\"2.0\">   
    <channel>   
    <title>Cute Puppies!</title>   
    <description>Great Pictures and Videos of Cute Puppies!</description>   
    <link>http://mydomain/cute_puppies/</link>   
    " ;   
    $sql = "select * from webref_rss_items";
    $LINK = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
    $rs = mysql_query($sql, $LINK);   
    while($row=mysql_fetch_array($rs)) {   
    echo "<item>\n";   
    echo "<title>".$row['title']."</title>\n";   
    echo "<description>".substr($row['description'],0,200)."...</description>\n";   
    echo "<link>".$row['link']."</link>\n";   
    echo "</item>\n";   
    }   
    echo " </channel> </rss> " ;   
    ?>
    PHP:
    one quick question:

    I have my htaccess set to this:

    AddType application/x-httpd-php .rss

    Is there anything else I need to add to the htaccess?
     
    megler, Mar 2, 2008 IP
  7. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Mysqli is the oop version of doing things like mysql_connect() and mysql_query()

    you can do things like $this->sql = new mysqli(connection details);

    I dont think you need anything else for .htaccess.

    Couldn't hurt to indent your code ;)
    Also, if you have 0 rows, or the database request fails, shouldnt you be showing an error message? :)
     
    matthewrobertbell, Mar 2, 2008 IP
  8. megler

    megler Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks for the clarification. yes, I should probably have better error messages. I'm ok at editing an existing script, but not very original at knowing what to add on my own. I'll get the error message in.

    thanks everyone for everything. I have a working script now. :D
     
    megler, Mar 2, 2008 IP