Forum Moderators: coopster

Message Too Old, No Replies

Insert RSS into MySQL

         

timmah1

7:17 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



I have been trying, unsuccessfully, for 2 days now trying to get this to work.

I think the problem lies with separating everything.

Here is my error


Query failed

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 '('Knicks claim Jason Williams', '

Update: The Knicks have the exclusiv' at line 1

Right now, there are 19 entries on the rss feed, and it gets stuck on the second one.

Here is my code


<?php
$xml=("http://www.example.com/xml/bbfpbasketballplayernews/node_feed/?User=bbfreepicks_nbaplayernews&pw=bbfpFFBP");

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

$db = mysql_connect("localhost", "#*$!x", "#*$!x");
mysql_select_db("#*$!x", $db);

//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('article');

$sql = "INSERT INTO feeds(title, content) VALUES";
for ($i = 0; $i <= 19; $i++)
//for ($i=0; $i<=19; $i++)
{
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->item($i)->getElementsByTagName('content')
->item(0)->childNodes->item(0)->nodeValue;

$sql .= "('{$item_title}', '{$item_desc}')";

}

$result = mysql_query($sql);

if(!$RESULT) {
echo "Query was successfull";
echo ("<p><a href='" . $item_link . "'>" . $item_title . "</a>");
echo ("<br />");
echo ($item_desc . "</p>");
}

else {
echo "Query failed
<blockquote>". mysql_error($db) ."</blockquote>";
}

?>

Can anybody PLEASE help me figure out this error?

Also, I enclosed my code in [code][/code], but it don't seem to be working
Thank you in advance

[edited by: eelixduppy at 11:00 pm (utc) on Aug. 3, 2009]
[edit reason] exemplified URL [/edit]

idfer

10:12 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



It's probably because some of the text contains single-quotes or newline characters that SQL chokes on. You need to pass the values through mysql_real_escape_string() when you're building your SQL statement, so change this:

$sql .= "('{$item_title}', '{$item_desc}')";

to this:

$sql .= "('" . mysql_real_escape_string($item_title). "', '". mysql_real_escape_string($item_desc) . "')";

Hope this helps.

timmah1

10:40 pm on Aug 3, 2009 (gmt 0)

10+ Year Member



thaks idfer,

It was actually two things, what you said made the difference though, I got it to work

Here is the working code for anybody that is interested


<?php
$db = mysql_connect("localhost", "#*$!xx", "#*$!xx");
mysql_select_db("#*$!xx", $db);

$xml=("http://www.example.com/xml/bbfpbasketballplayernews/node_feed/?User=bbfreepicks_nbaplayernews&pw=bbfpFFBP");

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('article');

$sql = "INSERT INTO feeds(title, content, date_posted) VALUES";

for ($i=0; $i<=19; $i++)
{
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('author')
->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->item($i)->getElementsByTagName('content')
->item(0)->childNodes->item(0)->nodeValue;
$sql .= "('" . mysql_real_escape_string($item_title). "', '". mysql_real_escape_string($item_desc) . "', '" . date("Y-m-d") . "')";

//echo ("<a href='" . $item_link . "'>" . $item_title . "</a>");
//echo ($item_desc);

if($i < 19) {
$sql .= ", ";
}
else {
$sql .=";";
}

}

$result = mysql_query($sql);
if(!$result) {
echo "Query was successfull";
}
else {
echo "Query failed<br />$d
<blockquote>". mysql_error() ."</blockquote>";
}
?>

[edited by: eelixduppy at 11:01 pm (utc) on Aug. 3, 2009]
[edit reason] exemplified URL [/edit]

eelixduppy

11:02 pm on Aug 3, 2009 (gmt 0)



Thanks for sharing, timmah1. And Welcome to WebmasterWorld! :)