如何在mysql中插入已解析的数据,但在我们需要检查记录是否存在之前。
我需要将项目解析的描述记录到具有已解析ID的项目。
例如,在我的数据库中,我有很多带有ID和另一列的产品,并且有空的"描述"柱。而且我需要感受这个空列与当前的decsription。
include 'simple_html_dom.php';
$db = mysqli_connect('localhost', 'mysql', 'mysql', 'database');
$site = 'http://optnow.ru/catalog';
$data = file_get_html($site);
$catalogLink = array();
$i = 0;
if(!empty($data)) {
foreach($data->find('div.cat-name a') as $catalog) {
$catalogLink['url'] = $catalog->href;
$urls[] = $catalogLink;
}
foreach($urls as $url => $k) {
foreach($k as $n) {
$catalogLink = 'http://optnow.ru/' . $n . '?page=0';
$productData = file_get_html($catalogLink);
foreach($productData->find('.link-pv-name') as $link) {
$productLink['url'] = $link->href;
$productUrls[] = $productLink;
}
foreach($productUrls as $href) {
$link = 'http://optnow.ru/' . $href['url'];
$product = file_get_html($link);
foreach($product->find('.block-d .btns-d .btn-buy') as $productId) {
if(!empty($productId)) {
$dataId = $productId->{'data-offerid'};
}
}
foreach($product->find('.description div div p') as $description) {
if(!empty($description)) {
$query = "INSERT INTO snowcore_parser_products (`description`) WHERE `remote_id` = " . $dataId . " VALUES (" . $description->plaintext . "');";
$sql = mysqli_query($db, $query);
print_r($sql);
}
}
}
}
}
}