How to delay INSERT time?

I’m using simplepie. I want to store the rss feeds items into mysql database.
I’ve been parsing every RSS items into $link,$title,$date,$content.
But simplepie get the rss feeds from its resource need few seconds and insert into just one moment, that cause it always stores first few lows of RSS items, not all of them.
How to delay INSERT time that can store all of the rss items into mysql database?TXS

Have not idea what you talking about but if you need to delay execution of anything you can just use usleep(500);
this will pause your script for half a second
just stick this like in your script just before the your insert code, it will wait half a second, then proceed further

Sorry for my poor english, I use below code.
When I run this php script, I checked my mysql, sometimes it just insert 8 items of 10, sometimes only 6 items of 10.
So I think insert into is faster than simplepie get all the rss items from the rss sourse.
How to delay the insert time that can insert all of the items?


<?php
require_once ('condatabase.php'); 
require_once ('simplepie.inc');

$url = 'http://news.google.com/news?pz=1&cf=all&ned=jp&hl=jp&topic=w&output=rss';
 
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->init();
 
// default starting item
$start = 0;
 
// default number of items to display. 0 = all
$length = 10; 
 
// if single item, set start to item number and length to 1
if(isset($_GET['item']))
{
        $start = $_GET['item'];
        $length = 1;
}
 
// set item link to script uri
$link = $_SERVER['REQUEST_URI'];
 
// loop through items
foreach($feed->get_items($start,$length) as $key=>$item)
{
 
        // set query string to item number
        $queryString = '?item=' . $key;
 
        // if we're displaying a single item, set item link to itself and set query string to nothing
        if(isset($_GET['item']))
        {
                $link = $item->get_link();
                $queryString = '';        
        }
 
        // insert item into datebase 

 mysql_query("INSERT INTO rss (link, title, date, content) VALUES ('".$item->get_link()."', '".$item->get_title()."', '".$item->get_date()."', '".$item->get_content()."')");

}
 
?>

I found set_cache_duration(), maybe upping that will help.

Sorry but it’s not your poor English, it’s your poor understanding of programming that’s the problem here.

php executes sequentially, so there is no way for php to insert an item ‘too fast’
The way that foreach loop works is that it will NOT proceed to the next iteration until the code inside the block has finished doing what it’s doing, in you case, it will not proceed to next item until the insert is complete.

There is also no way for insert to begin before the item is available for the mysql_query() function

You problem is definitely somewhere else, and has nothing to do with how fast mysql inserts a row.

You best bet would be to add some type of logging to log everything from your script to a text file so that you can examing the log and see which items has been parsed, see items before and after they have been inserted.

Another possibility is that mysql simply refuses to insert duplicate item, like the item with the same unique key.

Check your mysql table to see if it has any keys in that table defined as unique

Perhaps you already fetched and inserted the others?

Thanks to all.
I add $feed->set_cache_duration(5400); but it still not work well.
I changed another rss feed url, it has 20 feeds items.
I run my script first time, it inserted 7 items into database. Then when I empty the database and run the script second time, it inserted 10 items into database.
the insert number is not stable, but it always can not insert all of them.

You need some type of debugging. The simplest one is to just use echo to output some info the the screen.
For example, stick this before the mysql_insert
echo FILE.’ '.LINE.'before insert link: ‘.$item->get_link().’ title: ‘.$item->get_title().’ content: ‘.$item->get_content()."
";
Then see what is about to be inserted. There are many ways why data may not be inserted, maybe data in item content needs to escape some quotes first.
assing the result of insert to $res var like this
$res = mysql_query(…)
Then after the mysql_query add another echo
echo FILE.’ ‘.LINE.’ inserted item '.$res."
";
see if query was successful.

Hey, I added echo behind insert, then compared the results. I find something. When the article has ’ or " ,I can not find them in the database. The quotation mark made the mistake.
Maybe I should add some:
$str = preg_replace(‘/\’/‘, "\’“, $str);
$str = preg_replace('/\”/', ‘\"’, $str);

Thanks, I’m continuing my test. Late I will tell you results.
:slight_smile:

Welcome to the world of debugging. Just as I suspected, unescapted quote was causing the error.

Listen, the whole code, the way you doing things, starting with the idea to use Simplepie for rss parsing is wrong, I mean wrong to me and I’m sure to many people on this forum.
This is why I’m not going to rewrite the entire code for you, instead I will just show you a quick and dirty way to fix it:

Wrap the $item->get_content() inside the mysql_real_escape_string() statement, like this
mysql_real_escape_string($item->get_content())
You should be fine, do the same for title just to be sure.

This will fix you problem. In case you wondering about a better way to do this, here is the clue: use PDO and prepared statement

Thanks lampcms.com, this is a good way to teach the newer like me.
Point the right way is better to write a whole code.
٩(•̮̮̃•̃)۶
~