M
M
Mykola2014-07-10 21:05:45
PHP
Mykola, 2014-07-10 21:05:45

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;
  }
}

And one more question. How to get, say, the value of name knowing id, but not knowing the index. Other than a conditional loop?
array example: images.vfl.ru/ii/1405015514/4cec0414/5671518.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery Makarov, 2014-07-10
@iSensetivity

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 question

Ask a Question

731 491 924 answers to any question