gpt4 book ai didi

php - 脚本不创建表

转载 作者:行者123 更新时间:2023-11-29 21:33:02 24 4
gpt4 key购买 nike

我编写此脚本是为了从 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);

}

?>

最佳答案

这些问题与缺少 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."')");

关于php - 脚本不创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35107745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com