timmah1 Posted August 3, 2009 Share Posted August 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168709-solved-separating-statements/ Share on other sites More sharing options...
phpknight Posted August 3, 2009 Share Posted August 3, 2009 You just need to put a comma between each set of insert values: rosters. '), ('Knicks claim Quote Link to comment https://forums.phpfreaks.com/topic/168709-solved-separating-statements/#findComment-890121 Share on other sites More sharing options...
timmah1 Posted August 3, 2009 Author Share Posted August 3, 2009 And I do that how? It's not as simple as just putting a comma in the code Quote Link to comment https://forums.phpfreaks.com/topic/168709-solved-separating-statements/#findComment-890125 Share on other sites More sharing options...
phpknight Posted August 3, 2009 Share Posted August 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168709-solved-separating-statements/#findComment-890128 Share on other sites More sharing options...
phpknight Posted August 3, 2009 Share Posted August 3, 2009 One more thing. I would try putting your statement through phpMyAdmin. Remove and escape characters until it actually works. Then, you'll at least know what is causing it. Quote Link to comment https://forums.phpfreaks.com/topic/168709-solved-separating-statements/#findComment-890129 Share on other sites More sharing options...
TeNDoLLA Posted August 3, 2009 Share Posted August 3, 2009 I dont know for sure if this is any help but if your strings containts single quotes it might help escaping them.. using mysql_real_escape_string, addslashes or something similar to the values being inserted. Quote Link to comment https://forums.phpfreaks.com/topic/168709-solved-separating-statements/#findComment-890130 Share on other sites More sharing options...
timmah1 Posted August 3, 2009 Author Share Posted August 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168709-solved-separating-statements/#findComment-890138 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.