Answer the question
In order to leave comments, you need to log in
Is the algorithm optimal?
Task:
It is necessary to make an insertion of the parsed data into the database.
Data from different pages may be duplicated. In short, I take an array of parsed data (id) and make a selection of ids through where in. Then I compare 2 arrays for availability.
Flowchart: images.vfl.ru/ii/1405015366/8c9bde3b/5671493.png
function select_where_in($ids, $tbl_name, $id_text){
$pref = 'mal_';
$string = "SELECT `$id_text`, `$pref$id_text` FROM `$tbl_name` WHERE `$pref$id_text` IN (";
$onlyid = array();
foreach($ids as $id){
$string .= "$id[id], ";
$onlyid[] = $id['id'];
}
$string = rtrim($string, ', ');
$string .= ');' ;
$ss = mysql_query($string);
$num_rows = mysql_num_rows($ss);
if(!empty($num_rows)){
while ($r = mysql_fetch_row($ss)) {
$item['id'][] = $r[0];
$item['mal'][] = $r[1];
}
$result = array_diff ($onlyid, $item['mal']);
if(empty($result)) return 1; else return $result;
} else {
return 0;
}
}
Answer the question
In order to leave comments, you need to log in
I usually use INSERT IGNORE, the `ID` field in the table is set to primary. If there is already a record with this ID, then a new one is not inserted.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question