php - Date filter for inserting into DB -
i have built simple crawler 1 of our clients. facing issues duplicate entries in database.
basically doing looking website has lot of houses sale , pulling there address, postcode, town, price , status.
later when inserting database generating creation_date
.
the reason name can duplicate in case has been inserted @ least 2 years ago. 1 house can twice in database, long creation dates within minimum of 2 years range.
<?php //comparison current houses $query = mysql_query("select street, postcode, town, price, status, creation_time, print_status house"); // selecting table if (!$query) { die('invalid query: ' . mysql_error()); // checking errors } while ($row = mysql_fetch_array($query)) { // $row['street']; // $row['postcode']; // $row['town']; // $row['price']; // $row['status']; $creation_time = $row['creation_time']; $print_status = $row['print_status']; $c = 0; foreach ($houses $house) { $creation_time_u = strtotime($creation_time); // makes creation time unix $life_time = strtotime('+2 years', $creation_time_u); // calculates +2 years creation time if (($row['street'] == $house[0]) && ($row['postcode'] == $house[1]) && ($row['town'] == $house[2]) && ($life_time >= $now)) { unset($houses[$c]); // maybe use implode? when unset leaving array values gone, empty row } } $c++; $houses = array_values($houses); // fixes broken index after using unset } ?>
after has been completed, insert new $houses array database , print, next step kind of irrelevant in case.
so, don't know going wrong. if run twice in row, doesn't enter duplicate entries if run next day or something.
it makes same entry double. here example of found in database:
screenshot
so yeah, have spent time looking @ code , can't figure out why filter not working. expect has how managing time, not sure.
please advice!
instead of calculationg time-interval in php should select relevant houses in sql-query (see date_add here):
select street, postcode, town, price, status, creation_time, print_status house join house b on a.street = b.street , a.postcode = b.postcode , a.town = b.town a.creation_time <= date_add(creation_time, interval 2 years) -- select duplicates
Comments
Post a Comment