You are here: Re: XML to mySQL - Loop issue? « PHP Programming Language « IT news, forums, messages
Re: XML to mySQL - Loop issue?

Posted by bmichel on 10/28/07 19:23

On Oct 28, 7:15 pm, Sarah <Hershey...@aol.com> wrote:
> Hi -
>
> I am relatively new to PHP and mySQL. I am trying to loop through a
> bunch of
> XML feeds, pull out some info and place it into a mySQL table. I am
> able to loop through the feeds, pull out all the relevant info and
> print
> it to my browser with no problem using simpleXML for the parsing.
> When I try inserting the variables into mySQL instead of printing the
> variables to the browser it will only pull the first item and date
> from each XML page instead of all of the data. I have no idea
> why this would be. Any ideas? Here is the code I have so far:
>
> <?php
> $con2 = mysql_connect("localhost","USER", "PASS");
> if (!$con2)
> {
> die('Could not connect: ' . mysql_error());
> }
> mysql_select_db("DATABASE", $con2);
>
> $entriesinsert = 0;
>
> $resultBlogsMarket = mysql_query("SELECT * FROM myspacepages");
>
> while($rowMarket = mysql_fetch_array($resultBlogsMarket))
> {
> $myspaceid = $rowMarket['friendid'];
> $myspacemarket = $rowMarket['market'];
> $myspaceblogURL = "http://blog.myspace.com/blog/rss.cfm?friendID=".
> $myspaceid;
>
> // Load and parse the XML document
> $rss = simplexml_load_file($myspaceblogURL);
>
> // Here we'll put a loop to include each item's title and date
> foreach ($rss->channel->item as $item)
> {
> $titlequotes = "'".$item->title."'";
> $formattedDate = "'".date("Y-m-d H:i:s", strtotime($item->pubDate))."'";
>
> $myspaceidquotes = "'".$myspaceid."'";
>
> $resultBlogs = mysql_query("SELECT friendid, title, pubDate FROM
> blogs WHERE friendid=$myspaceidquotes AND pubDate=$formattedDate AND
> title=$titlequotes");
>
> if (mysql_num_rows($resultBlogs)=="0")
> {
> mysql_query("INSERT INTO blogs (friendid, title, pubDate)
> VALUES ($myspaceidquotes, $titlequotes, $formattedDate)");
> $affectedrows=mysql_affected_rows();
> if($affectedrows=="1")
> {
> $entriesinsert++;
> }
> }
> }
> }
>
> echo $entriesinsert." entries inserted. Done!";
>
> mysql_close($con2);
> ?>

I have the feeling it's because mysql_num_rows($resultBlogs)=="0" is
resolving to true after each first iteration.

This might be because:
"SELECT friendid, title, pubDate FROM
blogs WHERE friendid=$myspaceidquotes AND pubDate=$formattedDate AND
title=$titlequotes"
is the same on each iteration.

Try to print it out to the browser for each iteration. If it's the
same for all iterations, then most probably your query's logic is
wrong. Another possibility is that these values are not being updated
correctly: $myspaceidquotes, $formattedDate, $titlequotes
Print them to the browser and see if it's what you expect.

Additional Note: Good practice is to free your mysql results once
you're finished with them, add:
mysql_free_result($resultBlogsMarket) after the end of the outer loop.
mysql_free_result($resultBlogs) before the end of the inner loop.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация