我有以下代码:
foreach($RSS_DOC->channel->item as $RSSitem) {
$item_id = md5(str_replace(array('\'', '"'), '', $RSSitem->title));
$fetch_date = date("Y-m-j G:i:s"); //NOTE: we don't use a DB SQL function so its database independant
$item_title = str_replace(array('\'', '"', '(', ')', ' - ', '.'), '', $RSSitem->title);
$item_date = date("Y-m-j G:i:s", strtotime($RSSitem->pubDate));
$item_url = $RSSitem->link;
$words = explode(' ', $item_title);
echo "Processing item '" , $item_id , "' on " , $fetch_date , "<br/>";
echo $item_title, " - ";
echo $item_date, "<br/>";
echo $item_url, "<br/>";
// Does record already exist? Only insert if new item...
$item_exists_sql = "SELECT item_id FROM rssingest where item_id = '" . $item_id . "'";
$item_exists = mysql_query($item_exists_sql, $db);
if(mysql_num_rows($item_exists) < 1) {
echo "<font color=green>Inserting new item..</font><br/>";
$item_insert_sql = "INSERT INTO rssingest(item_id, feed_url, item_title, item_date, item_url, fetch_date) VALUES ('" . $item_id . "', '" . $feed_url . "', '" . $item_title . "', '" . $item_date . "', '" . $item_url . "', '" . $fetch_date . "')";
$insert_item = mysql_query($item_insert_sql, $db);
foreach ($words as $value) {
$word_exists_sql = "SELECT id FROM words WHERE word = '" . $value . "'";
$word_exists = mysql_query($word_exists_sql, $db);
if (mysql_num_rows($word_exists) < 1) {
$word_insert_sql = "INSERT INTO words(word, count) VALUES ('" . $value . "', '1')";
$insert_word = mysql_query($word_insert_sql, $db);
echo "<font color=green>Inserting word ".$value."..</font><br/>";
} else {
$word_update_sql = "UPDATE words SET count = count+1 WHERE word = '" . $value . "'";
$update_word = mysql_query($word_update_sql, $db);
echo "<font color=green>Updating count for word ".$value."..</font><br/>";
}
}
} else {
echo "<font color=blue>Not inserting existing item..</font><br/>";
}
echo "<br/>";
}
第一个foreach按预期工作,第二个只在DB中插入第一个rss项的第一个单词,即使正确显示其余单词的echo也是如此。
我错过了什么?
答案 0 :(得分:1)
$word_insert_sql = "INSERT INTO words(word, count) VALUES ('" . $value . "', count+1)";
count + 1
应该来自哪里。当然应该只是1
。
在任何情况下,如果您尝试进行数据库操作,在继续操作之前检查他们是否已经工作通常是一个好主意。