2012-05-10 116 views
1

快速问题在这里,我有一个运行过程,抓取RSS源并将它们添加到mySQL数据库。等待PHP中的任务完成,然后再转到下一个项目

在此过程中,我将使用Readability API抓取URL内容。

现在,这对单个条目工作正常,但由于此脚本可以有数百个条目,因此没有任何内容正在插入到我的数据库中。

我想知道它是否没有机会完成该过程,并立即跳到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语句,并且有根本就没有这样的记录与correspoding ITEM_ID更新? 尝试改变更新查询插入...对重复密钥更新

不幸的是,我们不知道你的数据库方案,但这样的事情应该工作:

$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); 
相关问题