Jump to content

Recommended Posts

I have been trying very hard to get this script to work but I am no good at arrays...

 

Anyhow, the basic idea is that the script will read an RSS feed and post all that content to the database which will be used to display content on the site.

 

<?php

$doc = new DOMDocument();
$doc->load('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
	$itemRSS = array ( 
		'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
		'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
		'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
		'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
		);
	array_push($arrFeeds, $itemRSS);
}

include 'dbconnect.php';

foreach ($arrFeeds as $item) {
$sql="INSERT INTO tb_rss (title, desc, link, date) VALUES ($item => title), $item => desc, $item => link, $item => date)";
	if (!mysql_query($sql)){ 
		die('Error: ' . mysql_error()); 
	}
}
?>

 

Current error messege is:

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 'desc, link, date) VALUES (Array => title), Array => desc, Array => link, Array =' at line 1

 

Help please?

Link to comment
https://forums.phpfreaks.com/topic/212339-rss-to-mysql/
Share on other sites

this part is not correct:

 ($item => title), $item => desc, $item => link, $item => date)

should be

$item['title'] etc.

 

Yeh, I noticed this myself but I am still getting the same error. Theres something up with the SQL query or something.  I have no idea what it is though  :shrug:

 

EDIT:

Current status on the script:

<?php

$doc = new DOMDocument();
$doc->load('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
	$itemRSS = array ( 
		'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
		'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
		'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
		'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
		);
	array_push($arrFeeds, $itemRSS);
}

include 'dbconnect.php';

foreach ($arrFeeds as $item) {
$sql="INSERT INTO tb_rss (title, desc, link, date) VALUES ($item[title], $item[desc], $item[link], $item[date])";
	if (!mysql_query($sql)){ 
		die('Error: ' . mysql_error()); 
	}
}
?>

 

Current error:

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 'desc, link, date) VALUES (Carson out of England qualifier, Goalkeeper Scott Cars' at line 1

Link to comment
https://forums.phpfreaks.com/topic/212339-rss-to-mysql/#findComment-1106372
Share on other sites

notice that I put

$item['title']

not

$item[title]

 

If I do that I get:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

I tried using "mysql_real_escape_string" already and its not working still. I looked at the article too and theres no quotes and stuff around where it says that error.

Link to comment
https://forums.phpfreaks.com/topic/212339-rss-to-mysql/#findComment-1106381
Share on other sites

try with something like this

VALUES(" . mysql_real_escape_string($item['title']) . ", " . mysql_real_escape_string($item['title']). ", " etc.

 

Still the same error.

 

could you post your new script ?

 

<?php

$doc = new DOMDocument();
$doc->load('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
	$itemRSS = array ( 
		'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
		'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
		'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
		'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
		);
	array_push($arrFeeds, $itemRSS);
}

include 'dbconnect.php';

foreach ($arrFeeds as $item) {

$sql="INSERT INTO tb_rss (title, desc, link, date) VALUES(" . mysql_real_escape_string($item['title']) . ", " . mysql_real_escape_string($item['desc']). ", " . mysql_real_escape_string($item['link']). ", " . mysql_real_escape_string($item['date']). ")";
	if (!mysql_query($sql)){ 
		die('Error: ' . mysql_error()); 
	}
}
?>

 

Error:

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 'desc, link, date) VALUES(Carson out of England qualifier, Goalkeeper Scott Carso' at line 1

Link to comment
https://forums.phpfreaks.com/topic/212339-rss-to-mysql/#findComment-1106393
Share on other sites

could you give this a try:

$sql="INSERT INTO tb_rss (title, desc, link, date) VALUES('" . mysql_real_escape_string($item['title']) . "', '" . mysql_real_escape_string($item['desc']). "', '" . mysql_real_escape_string($item['link']). "', '" . mysql_real_escape_string($item['date']). "')";

Link to comment
https://forums.phpfreaks.com/topic/212339-rss-to-mysql/#findComment-1106395
Share on other sites

could you give this a try:

$sql="INSERT INTO tb_rss (title, desc, link, date) VALUES('" . mysql_real_escape_string($item['title']) . "', '" . mysql_real_escape_string($item['desc']). "', '" . mysql_real_escape_string($item['link']). "', '" . mysql_real_escape_string($item['date']). "')";

 

Same error:

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 'desc, link, date) VALUES('Carson out of England qualifier', 'Goalkeeper Scott Ca' at line 1

Link to comment
https://forums.phpfreaks.com/topic/212339-rss-to-mysql/#findComment-1106400
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.