Trouble importing rss to MySQL

Status
Not open for further replies.

glew77

New member
Feb 13, 2008
20
0
0
I'm just learning PHP and am trying to import rss feeds into a database with this script i found but I keep getting this error

"Fatal error: Call to undefined method RSSDB::clearRssItems() in C:\wamp\www\test\rss2mysql.php on line 263"

Here's the whole script line 263 is near the bottom in green!


Code:
// Settings 
//
$file = "http://rss.businessweek.com/bw_rss/classiccars";

$db_hostname = "localhost";
$db_username = "";
$db_password = "";
$db_database = "rss";
/////////////////////////////////////////////////////////////////////////////

error_reporting(E_ALL);

/**
 * Database wrapping object for storing RSS items
 */
class RSSDB {

    /**
     * Set up the database connection.
     * @param $db_hostname string with the hostname to which to connect.
     * @param $db_username string with the username with which to connect.
     * @param $db_password string with the password with which to connect.
     * @param $db_database string with the name of the database to use.
     */
    function RSSDB($db_hostname, $db_username, $db_password, $db_database) {
        $this->db_hostname = $db_hostname;
        $this->db_username = $db_username;
        $this->db_password = $db_password;
        $this->db_database = $db_database;

        if (!$this->checkExtension()) {
            print("Couldn't load the MySQL connector, check your PHP installation. Aborting.");
            exit(-1);
        }
        if (!$this->getConnection()) {
            print("Couldn't connect to the database, aborting.");
            exit(-1);
        }
    }
    
    /**
     * Check to see if the MySQL extension is loaded. If not, try to load it.
     */
      function checkExtension() {
        if (!extension_loaded("mysql")) {
            if (!@dl("mysql.so")) {
                return(false);
            }
        }
        return(true);
    }

    /**
     * Try to establish a connection to the database server. 
     * @return true if successful, false otherwise.
     * @return Modifies $this->dbConn
     */
    function getConnection() {
        $this->dbConn = mysql_connect ($this->db_hostname, $this->db_username, $this->db_password);
        if (!$this->dbConn) {
            return(false);
        }
        if (!mysql_select_db($this->db_database, $this->dbConn)) { 
            return(false);
        }
        return(true);
    }
    
    /**
     * Perform a query on the database connection
     * @param qry String containing the query to execute. Remember to escape properly!
     * @return Doesn't return on qry error.
     */
    function query($qry) {
        if (!mysql_query($qry, $this->dbConn)) {
            print("Cannot execute query. Aborting.");
            exit(1);
        }
    }

    /**
     * Remove all items stored in the database.
     */
    function clearRssItems() {
        $this->query("DELETE FROM items");
    }

    /**
     * Store a single item in the database.
     * @param Lots, see definition. Everything is a string, exceptions:
     * @param $dc_date integer representing the date/time as a unix timestamp.
     */
    function saveRssItem($title, $link, $description, $dc_creator, $dc_date, $dc_subject) {
        $qry = "
            INSERT INTO items 
                (
                title, 
                link, 
                description, 
                dc_creator, 
                dc_date, 
                dc_subject) 
            VALUES
                (
                '".mysql_escape_string($title)."',
                '".mysql_escape_string($link)."',
                '".mysql_escape_string($description)."',
                '".mysql_escape_string($dc_creator)."',
                FROM_UNIXTIME(".mysql_escape_string($dc_date)."),
                '".mysql_escape_string($dc_subject)."'
                );
        ";

        $this->query($qry);
    }

}

/**
 * Quick'n'Dirty RSS reader and parser. Simply reads in the items in a feed. 
 */
class RSSParser {

    var $saveItems = array(
        "title"       => "string",
        "link"        => "string",
        "description" => "string",
        "dc:creator"  => "string",
        "dc:date"     => "date",
        "dc:subject"  => "string",
    );

    /**
     * Read and parse a RSS feed which $url points at (may be an URL or local file)
     * @param $url string containing the URL to the RSS feed. May be an URL or local file
     */
    function RSSParser($url) {
        $this->read($url);
        $this->readItems();

    }
    
    /**
     * Construct a new empty RSS item
     * @return a new empty assoc. array representing an rss item 
     */
    function newItem() {
        $retItem = array();

        foreach(array_keys($this->saveItems) as $key) {
            $retItem[$key] = "";
        }

        return($retItem);
    }

    /**
     * Read a RSS file/url and make a big string of it so it can be parsed.
     * @param $url string containing the URL to the RSS feed. May be an URL or local file
     */
    function read($url) {
        $this->rssData = implode("", file($url));
    }

    /**
     * Read <item> segments from the RSS file and stuff them in an array.
     */
    function readItems() {
        if (!isset($this->rssData)) {
            return(-1);
        }

        $this->rssItems = array();

        $parser = xml_parser_create();

        xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
        xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);

        xml_parse_into_struct($parser,$this->rssData,$values,$tags);
        xml_parser_free($parser);

        // Loop through all the elements in the RSS XML file.  If an <item> tag
        // is found, it's children will be added to a array until the closing
        // tag is found. Then the array is added to a list of items
        // ($this->rssItems).
        for ($i=1; $i < count($values); $i++) {

            $tagName = "";
            $tagType = "";
            $tagValue = "";

            if (array_key_exists("tag", $values[$i])) {
                $tagName = $values[$i]["tag"];
            }
            if (array_key_exists("type", $values[$i])) {
                $tagType = $values[$i]["type"];
            }
            if (array_key_exists("value", $values[$i])) {
                $tagValue = $values[$i]["value"];
            }

            if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "open") {
                // Looks like we found an <item> tag. Create a new array to
                // store it's children values as they will be found on the next
                // iteration of the loop.
                $rssItem = $this->newItem();
            }
            if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "close" && isset($rssItem)) {
                // </item> tag closed. Store the read item information.
                $this->rssItems[] = $rssItem;
                unset($rssItem); // No item information will be saved when this doesn't exist.
            }

            if (array_key_exists($tagName, $this->saveItems) && isset($rssItem)) {
                // Found a tag that we want to store and that's part of an
                // <item>. Save it.
                switch($this->saveItems[$tagName]) {
                    case "string":
                        $rssItem[$tagName] = $tagValue;
                        break;
                    case "date":
                        $rssItem[$tagName] = strtotime($tagValue);
                        break;
                    default:
                        print("Don't know how to handle type ".$this->saveItems[$tagName].". Aborting.");
                        exit(1);
                        break;
                }
            }
        }

    }
}

$db = new RSSDB($db_hostname, $db_username, $db_password, $db_database);
$parser = new RSSParser($file);

[COLOR=Lime]$db->clearRssItems(); // Delete old RSS items.[/COLOR]

foreach($parser->rssItems as $rssItem) {
    $db->saveRssItem(
        $rssItem["title"],
        $rssItem["link"],
        $rssItem["description"],
        $rssItem["dc:creator"],
        $rssItem["dc:date"],
        $rssItem["dc:subject"]
    );
}


I'd really appreciate any help on the error or if there is a better way!


 


fuck me!!!!

Sorry fucked this all up...the above is an error i got from trying to fix this damn thing :mad: the real problem i get is....

"Cannot execute query. Aborting."

Code:
    /**
     * Perform a query on the database connection
     * @param qry String containing the query to execute. Remember to escape properly!
     * @return Doesn't return on qry error.
     */
    function query($qry) {
        if (!mysql_query($qry, $this->dbConn)) {
            print("Cannot execute query. Aborting.");
            exit(1);
        }
    }
is where the error comes from.....can't edit the original thread so this is my problem....yes god i am an idiot....thanks for any help!
 
well, the problem is either in $qry or in you database connection info. Cant tell anymore than that from what you posted above.
 
I think its because you fucked up your query string on accident...
Code:
  $qry = "
            INSERT INTO items 
                (
                title, 
                link, 
                description, 
                dc_creator, 
                dc_date, 
                dc_subject) 
            VALUES
                (
                '".mysql_escape_string($title)."',
                '".mysql_escape_string($link)."',
                '".mysql_escape_string($description)."',
                '".mysql_escape_string($dc_creator)."',
                FROM_UNIXTIME(".mysql_escape_string($dc_date)."),
                '".mysql_escape_string($dc_subject)."'
                [COLOR=Blue][I][B]);[/B][/I][/COLOR]
        ";
Second last line ); remove the ;

Let us know.
 
thanks for the help guys, Rage9 removing that did not help though still got the same error. Here is the entire script

Code:
/* Database table structure:

CREATE TABLE `items` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `title` varchar(100) NOT NULL default '',
  `link` varchar(100) NOT NULL default '',
  `description` text NOT NULL,
  `dc_creator` varchar(100) NOT NULL default '',
  `dc_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `dc_subject` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

*/


/////////////////////////////////////////////////////////////////////////////
// Settings 
//
$file = "http://www.medicalnewstoday.com/medicalnews.xml";

$db_hostname = "localhost";
$db_username = "";
$db_password = "";
$db_database = "rss";
/////////////////////////////////////////////////////////////////////////////

error_reporting(E_ALL);

/**
 * Database wrapping object for storing RSS items
 */
class RSSDB {

    /**
     * Set up the database connection.
     * @param $db_hostname string with the hostname to which to connect.
     * @param $db_username string with the username with which to connect.
     * @param $db_password string with the password with which to connect.
     * @param $db_database string with the name of the database to use.
     */
    function RSSDB($db_hostname, $db_username, $db_password, $db_database) {
        $this->db_hostname = $db_hostname;
        $this->db_username = $db_username;
        $this->db_password = $db_password;
        $this->db_database = $db_database;

        if (!$this->checkExtension()) {
            print("Couldn't load the MySQL connector, check your PHP installation. Aborting.");
            exit(-1);
        }
        if (!$this->getConnection()) {
            print("Couldn't connect to the database, aborting.");
            exit(-1);
        }
    }
    
    /**
     * Check to see if the MySQL extension is loaded. If not, try to load it.
     */
    function checkExtension() {
        if (!extension_loaded("mysql")) {
            if (!@dl("mysql.so")) {
                return(false);
            }
        }
        return(true);
    }

    /**
     * Try to establish a connection to the database server. 
     * @return true if successful, false otherwise.
     * @return Modifies $this->dbConn
     */
    function getConnection() {
        $this->dbConn = mysql_connect ($this->db_hostname, $this->db_username, $this->db_password);
        if (!$this->dbConn) {
            return(false);
        }
        if (!mysql_select_db($this->db_database, $this->dbConn)) { 
            return(false);
        }
        return(true);
    }
    
    /**
     * Perform a query on the database connection
     * @param qry String containing the query to execute. Remember to escape properly!
     * @return Doesn't return on qry error.
     */
    function query($qry) {
        if (!mysql_query($qry, $this->dbConn)) {
            print("Cannot execute query. Aborting.");
            exit(1);
        }
    }

    /**
     * Remove all items stored in the database.
     */
    function clearRssItems() {
        $this->query("DELETE FROM items");
    }

    /**
     * Store a single item in the database.
     * @param Lots, see definition. Everything is a string, exceptions:
     * @param $dc_date integer representing the date/time as a unix timestamp.
     */
    function saveRssItem($title, $link, $description, $dc_creator, $dc_date, $dc_subject) {
        $qry = "
            INSERT INTO items 
                (
                title, 
                link, 
                description, 
                dc_creator, 
                dc_date, 
                dc_subject) 

            VALUES
                (
                '".mysql_escape_string($title)."',
                '".mysql_escape_string($link)."',
                '".mysql_escape_string($description)."',
                '".mysql_escape_string($dc_creator)."',
                FROM_UNIXTIME(".mysql_escape_string($dc_date)."),
                '".mysql_escape_string($dc_subject)."'
                )
        ";

        $this->query($qry);
    }

}

/**
 * Quick'n'Dirty RSS reader and parser. Simply reads in the items in a feed. 
 */
class RSSParser {

    var $saveItems = array(
        "title"       => "string",
        "link"        => "string",
        "description" => "string",
        "dc:creator"  => "string",
        "dc:date"     => "date",
        "dc:subject"  => "string",
    );

    /**
     * Read and parse a RSS feed which $url points at (may be an URL or local file)
     * @param $url string containing the URL to the RSS feed. May be an URL or local file
     */
    function RSSParser($url) {
        $this->read($url);
        $this->readItems();

    }
    
    /**
     * Construct a new empty RSS item
     * @return a new empty assoc. array representing an rss item 
     */
    function newItem() {
        $retItem = array();

        foreach(array_keys($this->saveItems) as $key) {
            $retItem[$key] = "";
        }

        return($retItem);
    }

    /**
     * Read a RSS file/url and make a big string of it so it can be parsed.
     * @param $url string containing the URL to the RSS feed. May be an URL or local file
     */
    function read($url) {
        $this->rssData = implode("", file($url));
    }

    /**
     * Read <item> segments from the RSS file and stuff them in an array.
     */
    function readItems() {
        if (!isset($this->rssData)) {
            return(-1);
        }

        $this->rssItems = array();

        $parser = xml_parser_create();

        xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
        xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);

        xml_parse_into_struct($parser,$this->rssData,$values,$tags);
        xml_parser_free($parser);

        // Loop through all the elements in the RSS XML file.  If an <item> tag
        // is found, it's children will be added to a array until the closing
        // tag is found. Then the array is added to a list of items
        // ($this->rssItems).
        for ($i=1; $i < count($values); $i++) {

            $tagName = "";
            $tagType = "";
            $tagValue = "";

            if (array_key_exists("tag", $values[$i])) {
                $tagName = $values[$i]["tag"];
            }
            if (array_key_exists("type", $values[$i])) {
                $tagType = $values[$i]["type"];
            }
            if (array_key_exists("value", $values[$i])) {
                $tagValue = $values[$i]["value"];
            }

            if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "open") {
                // Looks like we found an <item> tag. Create a new array to
                // store it's children values as they will be found on the next
                // iteration of the loop.
                $rssItem = $this->newItem();
            }
            if ($values[$i]["tag"] == "item" && $values[$i]["type"] == "close" && isset($rssItem)) {
                // </item> tag closed. Store the read item information.
                $this->rssItems[] = $rssItem;
                unset($rssItem); // No item information will be saved when this doesn't exist.
            }

            if (array_key_exists($tagName, $this->saveItems) && isset($rssItem)) {
                // Found a tag that we want to store and that's part of an
                // <item>. Save it.
                switch($this->saveItems[$tagName]) {
                    case "string":
                        $rssItem[$tagName] = $tagValue;
                        break;
                    case "date":
                        $rssItem[$tagName] = strtotime($tagValue);
                        break;
                    default:
                        print("Don't know how to handle type ".$this->saveItems[$tagName].". Aborting.");
                        exit(1);
                        break;
                }
            }
        }

    }
}

$db = new RSSDB($db_hostname, $db_username, $db_password, $db_database);
$parser = new RSSParser($file);

$db->clearRssItems(); // Delete old RSS items.

foreach($parser->rssItems as $rssItem) {
    $db->saveRssItem(
        $rssItem["title"],
        $rssItem["link"],
        $rssItem["description"],
        $rssItem["dc:creator"],
        $rssItem["dc:date"],
        $rssItem["dc:subject"]
    );
}

that's the whole thing from start to finish! I'm testing locally on wamp!
 
This is really only a guess but you are using the following code
Code:
$this->dbConn
The syntax "$this->anything" implies that you are calling or referencing a global property or method of that class.

However, I can't see anywhere that you have declared that variable or these for the following matter (you declare the bottom 4 outside the class)

Code:
$this->dbConn
$this->db_hostname
$this->db_username
$this->db_password
$this->db_database
I would suggest adding this variable declaration into your code

Code:
class RSSDB {
        private $dbConn;          
        private $db_hostname;
        private $db_username;
        private $db_password;
        private $db_database;
 
thanks ikonic, but it didn't work....or i just put it in the wrong place either way it still didn't work. I ran it thru nusphere debugger and it runs fine connects to db, grabs the feed....i can see all the information inside the array as i step thru the code, then it craps out near the end.....very frustrating :mad:. I will keep at it guys thanks for your help!
 
OK here are the basics on how to work through this as easily as possible. It's obviously an SQL problem.

If it doesn't pop off here:
Code:
if (!$this->getConnection()) {
            print("Couldn't connect to the database, aborting.");
            exit(-1);
        }
Then you have to check your query string:
Code:
$qry = "
            INSERT INTO items 
                (
                title, 
                link, 
                description, 
                dc_creator, 
                dc_date, 
                dc_subject) 

            VALUES
                (
                '".mysql_escape_string($title)."',
                '".mysql_escape_string($link)."',
                '".mysql_escape_string($description)."',
                '".mysql_escape_string($dc_creator)."',
                [COLOR=Blue]FROM_UNIXTIME(".mysql_escape_string($dc_date)."),[/COLOR]
                '".mysql_escape_string($dc_subject)."'
                )
        ";
The highlighted line is what jumps out at me as, might not be right. Set a breakpoint on this line, then take a step and look at the value of $qry. My thoughts are just double check what's been returned.

Oh BTW you should be using mysql_real_escape_string()
as mysql_escape_string() is depreciated.

Last bit we do know is that it pops off here:
Code:
  if (!mysql_query($qry, $this->dbConn)) {
            print("Cannot execute query. Aborting.");
            exit(1);
So if you query string is good, what else can be wrong? It could very well be the field sizes in the database. If a field size was set too small to handle the size of data you'd get an error. Check those also.

Now that you know what your looking for lets make a code change to help us track this down easier:
Code:
if (!mysql_query($qry, $this->dbConn)) {
            print("Cannot execute query. Aborting. ");
            [COLOR=Blue]echo mysql_errno($this->dbConn) . ": " . mysql_error($this->dbConn) . "\n";[/COLOR]
            exit(1);
That should tell you exactly what's wrong.
Hope that helps.
 
Fuck YES!!!!! Thanks very much Rage9 i added the error line and it told me it was a syntax error, then i changed the unixtime to mysql escape string and it worked like a fuckin charm thanks bro :bowdown: and to all that helped out!
 
Status
Not open for further replies.