1

这里有一个快速的问题,我有一个正在运行的进程,它可以抓取 RSS 提要并将它们添加到 mySQL 数据库中。

在此过程中,我将使用可读性 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";
}
4

2 回答 2

0

也许你的内存或时间用完了?启用警告和错误报告:

ini_set("display_errors", 1);
error_reporting(E_ALL);
于 2012-05-10T19:17:03.967 回答
0

可能没有插入任何内容,因为您使用的是 UPDATE 语句,并且根本没有相应的 item_id 记录要更新?尝试将 UPDATE 查询更改为 INSERT ... ON DUPLICATE KEY UPDATE

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

$item_insert_sql = "INSERT INTO tw_articles_parse (story_readability, item_id) VALUES ('$story', $item_id) ON DUPLICATE KEY UPDATE story_readability='$story'";
于 2012-05-10T19:21:29.597 回答