将数据插入表时,我无法获得以下代码以成功防止重复:
<?php
include('simple_html_dom.php');
// Create DOM from URL
$html = file_get_html('http://thesite.com/example/');
// Find all article blocks
$con = mysql_connect("localhost","admin","password");
mysql_select_db("dataminer", $con);
if (!$con)
{
die(mysql_error());
}else {
foreach($html->find('li.search-result') as $sitelisting){
$escapedlisting = mysql_real_escape_string($sitelisting);
$debugquery = mysql_query("INSERT IGNORE INTO extract (listing) VALUES ('$escapedlisting')");
if (!$debugquery)
{
die(mysql_error());
}
}
}
mysql_close($con);
?>
尽管有 INSERT IGNORE,但每次运行脚本时它仍然会复制每个条目。
编辑:通过更改解决此问题:
foreach($html->find('li.search-result') as $sitelisting){
$escapedlisting = mysql_real_escape_string($sitelisting);
$debugquery = mysql_query("INSERT IGNORE INTO extract (listing) VALUES ('$escapedlisting')");
至:
foreach($html->find('li.search-result') as $sitelisting){
$item['address'] = mysql_real_escape_string($sitelisting->find('div.adr-flag', 0)->plaintext);
$item['desc'] = mysql_real_escape_string($sitelisting->find('ul.attributes', 0)->plaintext);
$debugquery = mysql_query("INSERT IGNORE INTO extract VALUES ('$item[address]', '$item[desc]')");