The problem with your code is that you make N requests to the database where you can do with one.
$xml = simplexml_load_file("xmlfiles/".$row['idzk'].".xml"); $offers = iterator_to_array($xml->offer); if (count($offers) == 0) die('No ids'); $ids = array_map(function($offer){ //return (int)$offer['internal-id']; return "'".mysql_real_escape_string($offer['internal-id'])."'"; }, $offers); $in = implode(', ', $ids); $sql = "select `internal-id` from `object` where `internal-id` IN ($in)"; $r = mysql_query($sql); if (!$r) die(mysql_error()); $exists = []; while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) { $exists[$row['internal-id']] = true; } foreach ($offers as $offer) { if (isset($exists[$offer['internal-id']])) { echo "Найден {$offer['internal-id']}\n"; } else { echo "Не найден {$offer['internal-id']}\n"; } }
If there are too many lines, you can send in batches, say 1000
For a task from comments, you can create a unique index across the field internal-id and use the on duplicate key update construction:
$offers = iterator_to_array($xml->offer); $values = array_map(function($offer) { // нужно экранировать значения здесь по необходимости //return '('.implode(', ', [(int)$offer['internal-id'], (int)$offer['a']]).')'; return '('.implode(',', [ "'".mysql_real_escape_string($offer['internal-id'])."'", int($offer['a']), ]).')'; }, $offers); $values = implode(",\n", $values); $sql = <<<SQL INSERT INTO `object` (`internal-key`, `a`) VALUES $values ON DUPLICATE KEY UPDATE a = VALUES(a) SQL;
mysql_
Warning: This extension is deprecated since PHP 5.5.0 and removed in PHP 7.0.0. Use MySQLi or PDO_MySQL instead . See also the MySQL instruction : API selection and the corresponding FAQ for more details.
mysql_*to something more modern. And what’s the point ofphpmyadmin- teranв результате вместо 1 мы обновляем данные в БД, 2 добавляем новые- This should be in question! - vp_arth