Google Answers Logo
View Question
 
Q: Modify PHP - Mysql code ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: Modify PHP - Mysql code
Category: Computers > Programming
Asked by: shantanuo-ga
List Price: $10.00
Posted: 26 Jun 2004 02:06 PDT
Expires: 26 Jul 2004 02:06 PDT
Question ID: 366571
I want to install the RSS generation script written by Rogers Cadenhead.

http://www.cadenhead.org/workbench/gems/rss_2_php_script.txt
More info is available here...
http://www.cadenhead.org/workbench/stories/2004/05/22/publish-mysql-data-in-rss.html

When I use the code, I have to mention the column names. I have to
write a different code for each table.

Can some one write easier code that will read the column names and
generate RSS feed on the fly?
Answer  
Subject: Re: Modify PHP - Mysql code
Answered By: palitoy-ga on 27 Jun 2004 04:12 PDT
Rated:5 out of 5 stars
 
Hello Shantanuo

The code I have included in this answer is the solution to your
problem.  First of all I need to tell you how to drive the code.

The code takes a number of parameters from the URL, without these it
would be impossible for the script to run.  These parameters are:

db - this is the database name
dbt - this is the table in the database that contains the information
t - this is the column name that contains the <title> information for the RSS file
u - this is the column name that contains the <link> information for the RSS file
d - this is the column name that contains the <description>
information for the RSS file
id - this is the column name that the data will be sorted by, this is
usually an id tag or date in the database table so that the latest
results are used

Without passing these parameters the script would not know which
database, table or columns are the correct one.

The URL to run the script should be something like this:

http://www.thisistheurlofyourdomainname.com/rss.php?db=database_name&dbt=table_to_use_in_the_database&t=column_in_table_with_the_title_info&u=column_in_the_table_with_the_link_info&d=column_in_the_table_with_the_description_info&id=column_in_the_table_to_sort_by

For each RSS file you wish to create this URL will be slightly
different but you need only one rss.php file.

Finally the code requires one small edit on your part - you need to
put in the username and password for your MySQL database.  You can
change this in the code below - it only appears once on this line: $db
= mysql_pconnect("localhost", "yourusername", "yourpassword");

If you have any further questions on this please ask for clarification
and I will do my best to help.

The final code:

<?
/*****************************************************************************
 * rss.php
 * Version: 1.00
 * Author: Rogers Cadenhead
 * Date: 05/21/2004
 * http://www.cadenhead.org/workbench/
 *
 * Edited by palitoy-ga on Google Answers for shantanuo-ga
******************************************************************************
 This program is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 PARTICULAR PURPOSE.

 This work is hereby released into the Public Domain. To view a copy of the public
 domain dedication, visit http://creativecommons.org/licenses/publicdomain/ or
 send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
 94305, USA.
 ******************************************************************************/

// prepare HTML text for use as UTF-8 character data in XML
function cleanText($intext) {
    return utf8_encode(
        htmlspecialchars(
            stripslashes($intext)));
}

// set the file's content type and character set
// this must be called before any output
header("Content-Type: text/xml;charset=utf-8");

// get the names of the database table and columns
$name_db = $_GET["db"];
$name_dbt = $_GET["dbt"];
$name_title = $_GET["t"];
$name_url = $_GET["u"];
$name_desc = $_GET["d"];
$name_orderby = $_GET["id"];
// stop here if there is a parameter missing
if ( !$name_db or !$name_dbt or !$name_title or !$name_url or
!$name_desc or !$name_orderby ) {
   error_log("Error: One of the parameters required in the URL is missing.");
   exit;
}

// retrieve database records
$db = mysql_pconnect("localhost", "yourusername", "yourpassword");
// if there is an error stop here
if (!$db)
{
   error_log("Error: Could not connect to database in rss.php.");
   exit;
}

// store items from the database in the $result1 array
mysql_select_db($name_db);

$query1 = "select * from {$name_dbt} where {$name_title}!='' and
{$name_desc}!= '' and {$name_url}!= '' order by {$name_orderby} desc
limit 10";
$result1 = mysql_query($query1);
$phpversion = phpversion();

// display RSS 2.0 channel information

ECHO <<<END
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
   <channel>
      <title>This is the title of your website</title>
      <link>This is the link to your page</link>
      <description>A small description of your site</description>
      <language>en-us</language>
      <docs>http://backend.userland.com/rss</docs>
      <generator>PHP/$phpversion</generator>
END;

// loop through the array pulling database fields for each item
for ($i = 0; $i < mysql_num_rows($result1); $i++) {
   @$row = mysql_fetch_array($result1);
	print $row[$name_title];
   $title = cleanText($row[$name_title]);
   $link = cleanText($row[$name_url]);
   $description = cleanText($row[$name_desc]);
	$guid = $row[$name_orderby];

// display an item
ECHO <<<END

      <item>
         <title>$title</title>
         <link>$link</link>
         <description>$description</description>
         <guid isPermaLink="false">$guid</guid>
      </item>
END;

}

ECHO <<<END

   </channel>
</rss>
END;

?>

Request for Answer Clarification by shantanuo-ga on 27 Jun 2004 05:48 PDT
Error Message: XML document must have a top level element. 

http://saraswaticlasses.com/myrss.php?db=broker&dbt=delhi&t=First_Name&u=amount&id=amount

I have added the login and password info correctly.
Please guide how to correctly install the script.

Request for Answer Clarification by shantanuo-ga on 27 Jun 2004 06:09 PDT
The code worked!
My mistake: in my last test I forgot to add the &d parameter.
You forgot: to close the <docs> tag!

Clarification of Answer by palitoy-ga on 27 Jun 2004 07:28 PDT
Thanks for the 5-star rating - it is much appreciated.  I don't know
how I missed that closing </docs> tag as I was only copying and
pasting from the original there :)

Good luck with your RSS feeds!
shantanuo-ga rated this answer:5 out of 5 stars
Got more than expected!

Comments  
Subject: Re: Modify PHP - Mysql code
From: crythias-ga on 26 Jun 2004 21:11 PDT
 
My guess is that this is not a $2 answer, is why you're not getting a
hit on it. If you can give more information on what you're doing, what
you've tried, etc... maybe you might get an answer. But then again,
the answerer only gets 75% for his/her effort, and since this is
programming and not a "look it up", your offer is very much lowball.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy