I have a Code which gets news from a RSS site, I want to add this to a DB. You see I'm going to run this for a few different news sites however I don't want it to double up with the same news item so I've got a URL check in place then if it's not there it adds it to the site.
However I'm getting a error. What is wrong?
This is the XML output
INSERT error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's team, the Bulldogs, belted the Wests Tigers 32-10.Referee Paul Simpkins gave U' at line 1
CREATE TABLE news_rss (
rss_id int(10) NOT NULL auto_increment,
image varchar(200) NOT NULL default '',
items varchar(200) NOT NULL default '',
i int(10) NOT NULL default '0',
link varchar(200) NOT NULL default '',
title varchar(200) NOT NULL default '',
description text NOT NULL,
KEY rss_id (rss_id)
<?php
set_time_limit(0);
$file = "http://syndication.apn.co.nz/rss/nzhrsscid_000000079.xml";
$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;
function startElement($parser, $name, $attrs) {
global $rss_channel, $currently_writing, $main;
switch($name) {
case "RSS":
case "RDF:RDF":
case "ITEMS":
$currently_writing = "";
break;
case "CHANNEL":
$main = "CHANNEL";
break;
case "IMAGE":
$main = "IMAGE";
$rss_channel["IMAGE"] = array();
break;
case "ITEM":
$main = "ITEMS";
break;
default:
$currently_writing = $name;
break;
}
}
function endElement($parser, $name) {
global $rss_channel, $currently_writing, $item_counter;
$currently_writing = "";
if ($name == "ITEM") {
$item_counter++;
}
}
function characterData($parser, $data) {
global $rss_channel, $currently_writing, $main, $item_counter;
if ($currently_writing != "") {
switch($main) {
case "CHANNEL":
if (isset($rss_channel[$currently_writing])) {
$rss_channel[$currently_writing] .= $data;
} else {
$rss_channel[$currently_writing] = $data;
}
break;
case "IMAGE":
if (isset($rss_channel[$main][$currently_writing])) {
$rss_channel[$main][$currently_writing] .= $data;
} else {
$rss_channel[$main][$currently_writing] = $data;
}
break;
case "ITEMS":
if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
$rss_channel[$main][$item_counter][$currently_writing] .= $data;
} else {
//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
$rss_channel[$main][$item_counter][$currently_writing] = $data;
}
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
if (count($rss_channel["ITEMS"]) > 0) {
for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
$link2 = $rss_channel["ITEMS"][$i]["LINK"];
mysql_connect (localhost, **, **);
mysql_select_db (**);
$result4 = mysql_query ("SELECT * from news_rss where link = '$link2'");
$row4 = mysql_fetch_array($result4);
$num_rows = mysql_num_rows($result4);
if ($num_rows != 0) {
echo "Added Already";
} else {
$items = $rss_channel["ITEMS"][$i]["ITEMS"];
$i = $i;
$link = $rss_channel["ITEMS"][$i]["LINK"];
$title = $rss_channel["ITEMS"][$i]["TITLE"];
$image = $rss_channel["ITEMS"][$i]["IMAGE"];
$description = $rss_channel["ITEMS"][$i]["DESCRIPTION"];
mysql_connect (localhost, **, **);
mysql_select_db (**);
$result = mysql_query ("INSERT INTO news_rss (`rss_id`,`image`,`items`,`i`,`link`,`title`,`description`) Values('','$image','$items','$i','$link','$title','$description')") or die("INSERT error: ".mysql_error());
}
}
}
// output as HTML
?>