2012-07-11 48 views
1

我正在一个正在拉动xml供稿的文件上每5分钟运行一次cronjob,此刻它只是将该供稿添加到数据库,因此我可能会得到下面的内容。避免在codeignitor活动记录的数据库中插入重复值

title, this is a new title 
title, this is another title 

但cron的运行时我有问题,我再次得到以下

title, this is a new title 
title, this is another title 
title, this is a new title 
title, this is another title 

,当它再次运行

title, this is a new title 
title, this is another title 
title, this is a new title 
title, this is another title 
title, this is a new title 
title, this is another title 

等等.....

我只希望它插入一个记录,如果他们不是已经在数据库中有相同的标题,所以我的cron作业将保持运行当它注意到一个不同的标题时,它将它插入到数据库中。

我已经试过很多不同的选项这样的not_like等

有人能告诉我这样做的最好办法???

三江源;)

我设法用下面的,但绝对不是最好的办法待办事项去做它

function addResults($data){ 

    $this->db->select('title'); 
     $this->db->from('articles'); 
     $this->db->where('title = ' . "'" . $data['title'] . "'"); 
     //$this->db->limit(1); 
     $query=$this->db->get(); 
     echo $query->num_rows(); 
     if($query->num_rows()){ 

    }else{ 
     echo 'New Record ' . $data['title']; 
     $this->db->set('date', 'NOW()', FALSE); 
      $this->db->insert('articles', $data); 
     } 

    } 

回答

2

我知道我是一个有点晚了党,但这里有一个稍微更好的方法来做到这一点:

function addResults($data) 
{ 
    $result = $this->db->get_where('articles', array('title ' => $data['title'])); 
    $inDatabase = (bool)$result->num_rows(); 

    if (!$inDatabase) 
    { 
     echo 'New Record ' . $data['title']; 
     $this->db->set('date', 'NOW()', FALSE); 
     $this->db->insert('articles', $data); 
    } 
} 
+0

+1给你。尝试和处理我的代码。 TNX – jned29 2014-10-16 02:39:40