To diminish queries, you could perhaps try it like that :
PHP Code:
$charinsqa = 'INSERT INTO userskills (characterid) VALUES '; $insert = ''; /* Check if users exist */ $existquerya = 'SELECT characterid FROM userskills WHERE characterid IN(' . implode(',',$charid). ')'; $existresulta = mysql_query($existresulta); /* Removing elements from $charid if retrieved from database */ if (mysql_num_rows($existresulta)) { /* Fetch request result */ while($row = mysql_fetch_array($existresulta)) { /* Search if element is in $charid */ $position = array_search($row['characterid'],$charid);
/* Found */ if ($position) { unset($charid["$position"]); } } } /* At this point $charid contains only new values or is empty, let's build the insert */ if ($charid) { for ($i = 0; $i < sizeof($charid); $i++) { /* Coma needed only after the first value */ if ($i != 0) { $insert .= ','; }
$insert .= '("'.$charid[$i].'")'; }
/* Let's insert new values in one query into the database */ $charinsqab = $charinsqa . $insert; mysql_query($charinsqab) or die('Insert failed: '.mysql_error()); }
unset($charinsqa); unset($charinsqab);
I have not tested it and not sure there are no errors, but with this code you would have only one select and one insert