2012-05-10 8 views
1

ここで簡単な質問があります。私はそのグラブRSSフィードを実行しているプロセスを持っていて、それらをmySQLデータベースに追加しています。PHPでタスクが完了するのを待ってから次のアイテムに移動する

このプロセスでは、Readability APIを使用してURLコンテンツを取得します。

これは1つのエントリでうまくいきますが、このスクリプトは何百ものエントリを持つことができるため、データベースに何も挿入されません。

私はそれがプロセスを終了し、すぐにRSSの次のエントリにスキップする機会を得ていないのだろうかと思います。

誰でも移動する前に終了させる方法を提案できますか?以下のコード:

$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"; 
} 
+0

すべてのそれらのタグが何をしているの? – PeeHaa

+1

古くからの新しいmysql_ *関数で新しいコードを書くのをやめてください。彼らはもはや維持されておらず、コミュニティは[非推奨プロセス](http://news.php.net/php.internals/53799)を始めました。代わりに、準備されたステートメントについて学び、[PDO](http://php.net/pdo)または[MySQLi](http://php.net/mysqli)を使用する必要があります。あなたが学びたい人は[ここではPDO関連の非常に良いチュートリアルです](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 –

+1

あなたのSETはstory_readability = '$ story'ではないはずです – gunnx

答えて

0

あなたはUPDATEステートメントを使用しているので、おそらく何も挿入されていないと、更新するのitem_idをcorrespodingではそのようなレコードが単に存在しませんか? DUPLICATE KEY UPDATE

INSERT ... ONするUPDATEクエリを変更してみてください残念ながら、私たちはあなたのデータベーススキーマを知らないが、このようなものは動作するはずです:

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

多分、あなたは記憶や時間がなくなっていますか?警告やエラー報告を有効にする:

ini_set("display_errors", 1); 
error_reporting(E_ALL);