I’m using the SimplePie (and have used MagPie) open source RSS parsers. I have been able to fetch an RSS feed and display the title, description and date on to a webpage.
I have attempted to insert data in to a MySQL database, but have failed in some ways, which leads to the confusion.
require_once 'rss_fetch.inc';
$url = 'http://macrumors.com/rss.xml';
$rss = fetch_rss($url);
echo "Site: ", $rss->channel['title'], "<br>";
foreach ($rss->items as $item ) {
$title = $item[title];
$url = $item[link];
$pub = $item[pubdate];
$desc = $item[description];
echo "<a href=$url>$title</a></li><br>";
echo "$pub<br>";
echo "<p>$desc</p><br>";
echo "<br>";
That is the code in order to fetch from the RSS feed (in this example I’m using Macrumors) and here is the code I’m using to insert in to a MySQL database.
mysql_query("INSERT INTO articles (title, linkto, published, contents) VALUES ('$title', '$url','$pub','$desc')");
Whilst I’m fetching and displaying at least 20 articles, when I refresh the script only 3 “articles” are inserted in to the database. If I remove the posting of the description in to contents from my MySQL query I am able to insert all of the titles and other data that I have fetched.
So I am unsure whether my problem is with my RSS script or the way I’m inserting in to the database.
Most likely you’re not using mysql_real_escape_string() to make sure that the text is escaped before putting it into your SQL string. If description contains any
', you’ll end up with something like;The string will end at the
'indon'tand the rest of the text will cause a syntax error.