0

I wrote this script to pull items out of an rss feed and dump them in a database. However, I run the script and it gives me no error messages and it claims to have entered the information correctly, yet when I enter phpMyAdmin and see if the table is there and the information is filled in I find that the table was never created in the first place. I can't figure out for the life of me where there is any issues but I'd assume that there is some problem with my CREATE TABLE line but i can't be sure.

<?php

$xml = file_get_contents('http://syracuse.craigslist.org/search/sss?userid=35116943&format=rss');

// Use cURL to get the RSS feed into a PHP string variable.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
    'http://syracuse.craigslist.org/search/sss?userid=35116943&format=rss');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);

function element_set($element_name, $xml, $content_only = false) {
    if ($xml == false) {
        return false;
    }
    $found = preg_match_all('#<'.$element_name.'(?:\s+[^>]+)?>' .
            '(.*?)</'.$element_name.'>#s',
            $xml, $matches, PREG_PATTERN_ORDER);
    if ($found != false) {
        if ($content_only) {
            return $matches[1];  //ignore the enlosing tags
        } else {
            return $matches[0];  //return the full pattern match
        }
    }
    // No match found: return false.
    return false;
}

function value_in($element_name, $xml, $content_only = true) {
    if ($xml == false) {
        return false;
    }
    $found = preg_match('#<'.$element_name.'(?:\s+[^>]+)?>(.*?)'.
            '</'.$element_name.'>#s', $xml, $matches);
    if ($found != false) {
        if ($content_only) {
            return $matches[1];  //ignore the enclosing tags
        } else {
            return $matches[0];  //return the full pattern match
        }
    }
    // No match found: return false.
    return false;
}

function value_in_image($xml, $content_only = true){
    if ($xml == false) {
        return false;
    }
    $found = preg_match('#<enc:enclosure resource="(.*)"#Us', $xml, $matches);
    if ($found != false) {
        if ($content_only) {
            return $matches[1];  //ignore the enclosing tags
        } else {
            return $matches[0];  //return the full pattern match
        }
    }
    // No match found: return false.
    return false;
}

$channel_title = value_in('title', $xml);

// An RSS 2.0 feed must also have a link element that
// points to the site that the feed came from.
$channel_link = value_in('link', $xml);
$news_items = element_set('item', $xml);


// Create an array of item elements from the XML feed.
include_once('mysql_connect.php');
$user_name = "root";
$password = "";
$database = "store-listings";
$server = "127.0.0.1";
$db_table = 'furniture';
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {

    mysql_query('DROP TABLE IF EXISTS `'.$database.'`.`'.$db_table.'`');
    mysql_query('CREATE TABLE '.$db_table.' (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(30) NOT NULL,
    url VARCHAR(30) NOT NULL,
    image VARCHAR(50),
    description VARCHAR(1000),
    timestamp TIMESTAMP)'
    );
    foreach($news_items as $item) {
        $title = value_in('title', $item);
        $url = value_in('link', $item);
        $description = value_in('description', $item);
        $timestamp = value_in('dc:date', $item);
        $image = value_in_image($item);
        $item_array[] = array(
            'title' => $title,
            'url' => $url,
            'description' => $description,
            'timestamp' => $timestamp,
            'image' => $image
        );

        $SQL = "INSERT INTO ".$db_table." (title, url, image, description, timestamp) VALUES ('".$title."', '".$url."', '".$image."', '".$description."', '".$timestamp."')";

    }   
    mysql_close($db_handle);

    print "Records added to the database";

}else{

    print "Database NOT Found ";
    mysql_close($db_handle);

}

?>

1 Answer 1

0

The issues were related to missing the final ) in the CREATE TABLE line. Also I created the mysql function as a variable without ever executing it.

So I changed:

$SQL = "INSERT INTO ".$db_table." (title, url, image, description, timestamp) VALUES ('".$title."', '".$url."', '".$image."', '".$description."', '".$timestamp."')";

To:

mysql_query("INSERT INTO ".$db_table." (title, url, image, description, timestamp) VALUES ('".$title."', '".$url."', '".$image."', '".$description."', '".$timestamp."')");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.