php - mysql中简单的html dom重复条目

标签 php html mysql database dom

我在使用简单的 html dom 时遇到问题

我的问题是..

我想从其他网站获取内容

但是当我插入数据库时​​,他是重复的数据或内容

以及如何没有重复的数据或条目

这是我的代码

include "simple_html_dom.php";
$html = new simple_html_dom();
$html = file_get_html('http://example/');


$items = $html->find('div[class=thumb-box latest-entries],div[class=thumb-box latest-entries no-margin]');  
$nomor=1;
// loop into each li element
//echo"<h1>class margin</h1>";
foreach($items as $i => $post) {
    // get the img
    $img = $post->find('img', 0)->src;

    // get the post's url       
    $url = $post->find('a', 0)->href;

    // get the title
    $title = $post->find('h2[class=article-heading]a', 1)->plaintext;

    // another way to get the title
   // $title2 = $post->find('div[article-text]', 0)->plaintext;

    // get the description
    $desc = $post->find('p[class=article-text]', 0)->plaintext;

    //get date
    $date = $post->find('span[class=date]', 0)->plaintext;

    //get comment
    $comment = $post->find('span[class=comment]', 0)->plaintext;

    //get view
    $view = $post->find('span[class=view]', 0)->plaintext;

    // Print all

   if($title != "" && $url !="" && $img !="" && $desc !="" && $date !="" && $comment !="" && $view !="")
    {
   echo "<h1>$nomor</h1>
            Title   :$title<br>
           Image    :$img<br>
           Url      :$url<br>
           Description :$desc<br>
           Date     :$date<br>
           Comment  :$comment<br>
           View     :$view<br>";
           $gambar =$img; 
           $link   =$url;
    echo "<hr/>";
    }
    $nomor++;
$display=mysql_query("select * from homepage where title='$title' && url='$url' && image='$img'");
if(mysql_num_rows($display)>0)  
{
echo"<b style='color:red;'>title and url has Registered in database</b>";
}
else
{
    mysql_query("INSERT INTO homepage (title,image,url,description,date,comment,view)values('$title','$gambar','$link','$desc','$date','$comment','$view')")or die("".mysql_error());
}

}

最佳答案

您是否向 phpmyadmin 检查过内容?它可能已添加到数据库中,但您的 $display 查询可能无法正常工作,因此它将始终有零行 => 正在尝试在已添加新条目时将其添加到数据库中。

关于php - mysql中简单的html dom重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25907445/

相关文章:

mysql - 连接到 Amazon RDS 实例的握手不活动超时

php - 加入两个mysql表并在php中填充一个数组

php - mpdf隐藏元素

php - 来自 MySQL 的订单

PHP PDO mysql_result() 等效吗?

Javascript Button onClick 在 Chrome 扩展弹出窗口中不起作用

php - 如何在 codeigniter 中重置密码后重定向

php - JQuery 和提交按钮

jquery - 如何居中对齐灯箱并隐藏滚动条

php - 在 INSERT INTO 查询期间读取自动增量列