脚本不创建表

时间:2016-01-30 22:07:33

标签: php mysql curl rss

我编写了这个脚本来从rss feed中提取项目并将它们转储到数据库中。但是,我运行脚本,它没有给我任何错误消息,它声称已正确输入信息,但当我输入phpMyAdmin并查看表是否存在并且信息已填入时我发现该表从未创建过第一名。我无法弄清楚我的生活中哪里有任何问题,但我认为我的CREATE TABLE行存在一些问题,但我无法确定。

<?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 个答案:

答案 0 :(得分:0)

问题与错过)行中的最终CREATE TABLE有关。另外,我创建了mysql函数作为变量而没有执行它。

所以我改变了:

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

要:

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