Вопрос

I'm reading and store a RSS feed in my database.I'm using this code

    <?php
      include_once 'db.php';
       $homepage = file_get_contents('http://rss.cnn.com/rss/edition_us.rss');

         $movies = new SimpleXMLElement($homepage);
          foreach($movies->channel->item as $opt){
            $title= $opt->title;
        $tittle=mysql_real_escape_string($title);
           $link=$opt->link;
           $links=mysql_real_escape_string($link);
           $des=$opt->description;
            $dess=mysql_real_escape_string($des);

           $sql="INSERT INTO store_feed (title, link, description)
             VALUES ('$tittle','$links','$dess')";

            $result=mysql_query($sql) or die('Error, insert query failed');
             }


              ?>

and table structure is

Table structure for table store_feed

 CREATE TABLE IF NOT EXISTS `store_feed` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(200) NOT NULL,
    `link` varchar(200) NOT NULL,
      `description` varchar(500) NOT NULL,
     `feedburner` varchar(200) NOT NULL,
     PRIMARY KEY (`id`)
       ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

now my requirement is when insert a new record if link is same then update only title and description of this field without insert record again. in other word I want to stop duplicate data if link is same.

Это было полезно?

Решение

Usually, I do something like this:

WARNING: UNTESTED CODE!

$sql = "SELECT id FROM store_feed WHERE link = '$links' LIMIT 1";
$rs = mysql_query($sql) or die( mysql_error() );
if( mysql_num_rows($rs) > 0)//we got link
{
    $r = mysql_fetch_array($rs);
    $id = $r['id'];
    $sql = "UPDATE store_feed SET title = '$title', description = '$dess' WHERE id = '$id'";
    mysql_query($sql) or die( mysql_error() );
} else {
    $sql = "INSERT INTO store_feed (title, link, description) VALUES ('$tittle','$links','$dess')";
    mysql_query($sql) or die( mysql_error() );
}

Другие советы

Before you inserting every feed into database just check title and description exist in database if not exist insert that row into database.

http://www.exceptionhandle.com

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top