Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 脚本不创建表_Php_Mysql_Curl_Rss - Fatal编程技术网

Php 脚本不创建表

Php 脚本不创建表,php,mysql,curl,rss,Php,Mysql,Curl,Rss,我编写这个脚本是为了从rss提要中提取项目并将其转储到数据库中。但是,我运行了脚本,它没有给我任何错误消息,它声称输入了正确的信息,但是当我输入phpMyAdmin并查看表是否在那里以及信息是否已填充时,我发现表一开始从未创建过。我一生都不知道哪里有问题,但我假设我的CREATE TABLE行有问题,但我不能确定 <?php $xml = file_get_contents('http://syracuse.craigslist.org/search/sss?userid=3511694

我编写这个脚本是为了从rss提要中提取项目并将其转储到数据库中。但是,我运行了脚本,它没有给我任何错误消息,它声称输入了正确的信息,但是当我输入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);

}

?>
$title,
“url”=>$url,
'description'=>$description,
'timestamp'=>$timestamp,
'image'=>$image
);
$SQL=“插入到“$db_表”。(标题、url、图像、描述、时间戳)值(“$title.”、“$url.”、“$image.”、“$description.”、“$timestamp.”);
}   
mysql\u close($db\u handle);
打印“添加到数据库的记录”;
}否则{
打印“未找到数据库”;
mysql\u close($db\u handle);
}
?>

这些问题与
创建表
行中缺少最后的
有关。我还创建了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."')");