Search code examples
phpmysqlrssreadability

Waiting for task in PHP to finish before moving to next item


Quick question here, i've got a process running that grabs RSS feeds and adds them to a mySQL database.

During this process I'll be using the Readability API to grab the URL content as I go.

Now this works fine on single entries, but as this script can have hundreds of entries, nothing is being inserting into my database.

I'm wondering if it's not getting a chance to finish the process and immediately skipping onto the next entry in the RSS.

Can anyone suggest a way of letting it finish before moving on? Code below:

$db_hostname="localhost";
$db_username="myusername";
$db_password="mypassword";

try
{
/*  query the database */

$db = mysql_connect($db_hostname,$db_username,$db_password);
if (!$db)
{
    die("Could not connect: " . mysql_error());
}
mysql_select_db("MyDB", $db);


// Get stories that don't have a the readability assigned
$query="select item_id, item_url from tw_articles_parse where story_readability = '' LIMIT 0 , 1";
$result=mysql_query($query);
$num=mysql_numrows($result);
// Close the DB connection
mysql_close();


// Start the loop of source RSS feeds
$i=0;
while ($i < $num) {

    $item_url=mysql_result($result,$i,"item_url");
    $item_id=mysql_result($result,$i,"item_id");

    // Parse the story URL into the Readability API
        $url = "https://www.readability.com/api/content/v1/parser?url=$item_url&token=myapikey";
        // Get the contents of the JSON returned by the API
        $json = file_get_contents($url);
        // Decode the JSON
        $out = json_decode($json, true);
        // Set the content as a variable
        $story = mysql_real_escape_string($out['content']);

        // Insert into the DB - Adding 0 to story_club_id as default
        $item_insert_sql = "UPDATE tw_articles_parse SET story_readability=$story WHERE item_id='" . $item_id . "'";
        $insert_item = mysql_query($item_insert_sql, $db);



$i++;
}// end the loop of feeds


   } catch (Exception $e)
{
echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Solution

  • Probably nothing is inserted because you are using UPDATE statement and there are simply no such records with correspoding item_id to be updated? Try changing UPDATE query to INSERT ... ON DUPLICATE KEY UPDATE

    Unfortunately we don't know your database scheme, but something like this should work:

    $item_insert_sql = "INSERT INTO tw_articles_parse (story_readability, item_id) VALUES ('$story', $item_id) ON DUPLICATE KEY UPDATE story_readability='$story'";