2010-09-16 165 views
0

Im使用tweets加载xml文件,然后将其添加到Mysql数据库。到目前为止它工作正常,但id喜欢对url字段进行基本检查,看它是否已经添加到数据库中。如果它在数据库中已经有id,就像更新提及计数一样,否则将它作为新条目插入。这是一个真正的新手问题,但我努力让它工作。下面是现在的代码:PHP/MySQL检查数据库中的重复内容

<?php 

require_once('functions/functions.php'); 

$dbhost = '****'; 
$dbuser = '****'; 
$dbpass = '****'; 

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); 

if($conn) 
{ 
    echo "Database Connection Successfull...<br /><br />"; 
} 

$dbname = '****'; 
mysql_select_db($dbname) or die('Couldnt connect to database table'); 

if($dbname) 
{ 
    echo "Database ".$dbname." Selected..<br /><br />"; 
} 

    $tweetmeme = "http://api.tweetmeme.com/stories/popular.xml?category=sports-soccer&count=30" ; 

    $xml = @simplexml_load_file($tweetmeme) or die ("no file loaded") ; 
    if($xml) 
    { 
    echo "Tweetmeme XML loaded with ".count($xml->stories->story)." stories in the file..<br /><br />"; 
    } 
if(get_magic_quotes_gpc()) 
{ 
    echo "Magic Quotes is ON<br /><br />"; 
} 
$count_insert=0; 
$count_update=0; 

//Select url and id and compare to the url being added 
$url_compare = mysql_query("SELECT id,url FROM ft_tweets"); 


if($url_compare) 
{ 
    //echo_result($url_compare); 
    echo mysql_num_rows($url_compare)." rows loaded.<br /><br />"; 
} 

$url_compare = mysql_fetch_assoc($url_compare); 

foreach($xml->stories->story as $story) 
{ 
    $title=check_input($story->title); 


    $url=check_input($story->url); 

    $media_type=check_input($story->media_type); 

    $created=check_input($story->created_at); 

    $url_count=check_input($story->url_count); 

    $comment_count=check_input($story->comment_count); 

    $excerpt=check_input($story->excerpt); 



    $sql = "INSERT INTO ft_tweets (title,url,media_type,created_at,mention_count,comment_count,excerpt) VALUES ($title,$url,$media_type,$created,$url_count   ,$comment_count,$excerpt)"; 


    $result = mysql_query($sql) or die(mysql_error()); 
    if($result) 
    { 
     //echo "added to database<br />"; 
     $count_insert++; 
    } 
} 
echo $count_insert." Records added to the database<br /><br />"; 
$search=mysql_query("SELECT * FROM ft_tweets WHERE title LIKE '%liverpool%' OR excerpt LIKE '%lfc%'"); 
if($search) 
{ 
    echo "<br /><br />Found tweets mentioning Liverpool"; 
} 
//fetch 10 tweets from ft_tweets where liverpool or lfc is in the excerpt or the title and store it in a variable 
//echo_result($search); 
echo "<br />"; 
var_dump($search); 
echo "<br />"; 
?> 

回答

1

好像你只需要首先检查重复的推文。

$sql = "SELECT id FROM ft_tweets WHERE url = '" . mysql_real_escape_string($url) . "'"; 
$duplicate = mysql_query($sql); 
if (mysql_num_rows($duplicate) > 0) { 
    // some notice about duplicate tweet 
    $count_duplicate++; 
} else { 
    // insert the new 
    $sql = "INSERT INTO..."; 
} 
+0

不适合我,它运行,但没有拿起任何重复的网址在数据库中。逻辑看起来很好,所以也许它的url格式之前,它进入数据库 – 2010-09-17 00:10:53

1

如果您设置网址为重点,你可以使用INSERT ... on DUPLACATE KEY UPDATE语法时才。

+0

这种解决方案是更优雅:)但你需要MySQL 5.0 + – 2010-09-16 23:47:32

+0

你是建议,而不是使用一个任意的主要编号的主要键你使用的URL或只是使该网址在数据库中有一个独特的属性 – 2010-09-17 00:12:44

+0

如果您使用独特的网址,你不需要使用其他IDS。 – jcubic 2010-09-17 08:24:36