PHP mysql_query completely ignores first query in loop?

  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

I can't figure this one out, and I can only believe that there's a bug in PHP for this one. I have a foreach loop that loops through an xml feed (my twitter feed) and prints out my latest tweets. I decided that I wanted to add database caching to that to help with bandwidth performance. I loop through and it prints out the 5 tweets just fine, but the mysql_query function is COMPLETELY ignored on the first loop and only runs that last 4. It's because it always returns false. Here's the code.
Code: [ Select ]
 
    foreach ($rss_xml->channel->item as $item){
        if($tweetsNum < 5){ //Read entire feed.
            $tweet = explode(": ", $item->title);
            $tweetmessage = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\" rel=\"nofollow\">$1</a>", $tweet[1]);
            $tweetmessage = preg_replace("/(@[^\s]+)/", "<span class=\"tweet_at\">$1</span>", $tweetmessage);
            $tweetsARR[$tweetsNum] = "$tweetmessage";
            $query = "INSERT INTO rss_cache (rss_name, post, post_time) VALUES('Twitter', '$tweetsARR[$tweetsNum]', '$timenow')";
            echo "$query<br/>";
            mysql_query($query);
            $tweetsNum++;
        }else{
            break;
        }
    }
 
  1.  
  2.     foreach ($rss_xml->channel->item as $item){
  3.         if($tweetsNum < 5){ //Read entire feed.
  4.             $tweet = explode(": ", $item->title);
  5.             $tweetmessage = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\" rel=\"nofollow\">$1</a>", $tweet[1]);
  6.             $tweetmessage = preg_replace("/(@[^\s]+)/", "<span class=\"tweet_at\">$1</span>", $tweetmessage);
  7.             $tweetsARR[$tweetsNum] = "$tweetmessage";
  8.             $query = "INSERT INTO rss_cache (rss_name, post, post_time) VALUES('Twitter', '$tweetsARR[$tweetsNum]', '$timenow')";
  9.             echo "$query<br/>";
  10.             mysql_query($query);
  11.             $tweetsNum++;
  12.         }else{
  13.             break;
  14.         }
  15.     }
  16.  


When I echo the query string it works fine, there are NO errors. Php just refuses to insert anything into the database on the first iteration always returning false. Help anyone!? :cry:
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 437

Post 3+ Months Ago

depending on what your $tweetsNum is initialized at you may only be getting 4 tweets from your xml. :-)

Try ($tweetsNum <= 5) or ($tweetsNum < 6).
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

No, that's not the issue. I'm echoing the query before I actually run the mysql_query on it to check for that very problem. It echo's five times and they're all correct. The problem is, is that the first time I mysql_query it returns false, while all the remaining times it returns true. For example, if I ran the loop 10 times, the first iteration it fails, then the remaining 9 it always works.

For some reason mysql_query is ALWAYS returning false the first iteration, which simple doesn't make sense. I know the query string is valid because I'm echoing it out before it runs.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 437

Post 3+ Months Ago

Can you echo the data that should be getting stored? I guess when you're echoing the query you're getting the value fields that should be stored so you probably would have already noticed some bad values from the first set of data.

Maybe your first iteration contains some unwanted data from the explode(). The separation of ": " may leave some trailing data in the first set of data that the db table does not like. It's a long shot but maybe change the value of the column "post" in your database to varchar(225) NULL and try to use trim() on the value that's getting stored to remove whitespace.

From the code posted the last thing I can think of is maybe trying to store the $query in an array and maybe loop through it making sure the mysql_query() is getting called for each query.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

What if you echo the first query? (That's if it's always the same).
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

Devilwood you were right with your last post. I was working through it and decided to insert some dummy posts. I noticed that it wasn't just the first post that wasn't being inserted anymore. Coincidentally, during my testing, whenever I went to test my application the first post always had a ' in it which caused the query to fail (though not giving me any warning) causing me to think there was something special about the first query.

It was just an unfortunate coincidence :P. A simple addslashes function set everything right again. I hate it when it's the obvious simple solution lol.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 437

Post 3+ Months Ago

I should have picked up on it quicker. That's happened to me tons. Especially when I was separating strings with some delimiter from plain text files in order to store sentence segments in a database. It seemed my cuts would get most of the data clean, but it was usually the first cut that had some error; like some extra space or like in your case an '. Glad you got it worked out. Feels good to move forward after something so aggravating. Atleast the fix was quick.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 41 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.