Jump to content

Recommended Posts

I believe I found the problem for my code, I just don't know how to fix it.

 

With my code

<?php
$db = mysql_connect("localhost", "xxxx", "xxxx");
mysql_select_db("xxxx", $db);

$xml=("http://www.novafantasysports.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) 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 .= "('{$item_title}', '{$item_desc}')";
  echo ("<a href='" . $item_link  . "'>" . $item_title . "</a>");
  echo ($item_desc);
  }
  
$result = mysql_query($sql);
if(!!$RESULT) {
echo "Query was successfull";
}  
else {
echo "Query failed<br />$d
<blockquote>". $sql ."</blockquote>";
}
?>

The query always fails

The results is this

Query failed
INSERT INTO feeds(title, content) VALUES('Cavs want Powe', 'Update: After failing to land Hakim Warrick, the Cavaliers have reportedly offered a contract to Leon Powe (knee), according to the Cleveland Plain Dealer. Cleveland is Powe's top choice to sign with, so it appears as if a deal could be imminent.  Fantasy Impact: Powe is a guy who will not be ready to begin the season, and there are a number of questions marks pertaining to his return.  His target is early 2010, but the big question being how effective he would be able to be.  Powe might be a nice mid-season addition to fantasy rosters.  ')('Knicks claim Jason Williams', '

 

Notice how it don't separate the 2?

rosters.  ')('Knicks claim

 

How can I get this code to separate all these?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/168709-solved-separating-statements/
Share on other sites

Unless I am missing something, it should be.  Try it with just values of:

player1, test1

player2, test2

 

See if that works. If not, you'll have to account for special characters or whatever is causing the issue.  First, you just need to make sure the INSERT syntax is right.

 

I got it to work by doing this

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

 

So, the working code is this

<?php
$db = mysql_connect("localhost", "xxxx", "xxxx");
mysql_select_db("xxxx", $db);

$xml=("http://www.novafantasysports.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) 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) . "')"; 

  //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>";
}
?>

 

Thanks for all the help

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.